ph94
ph94

Reputation: 27

Android - best way to remember date

What is the best way to remember date? I mean, I want to save last date when the app was started. I want use this information to check if the app wasn't ran for longer than one day. I was thinking about saving date in .txt file but maybe there is better way to do this?

Upvotes: 0

Views: 164

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338326

ISO 8601

While you could save the milliseconds count-from-epoch as the serialized value, I don't recommend that. Humans cannot easily understand the date-time meaning of a 64-bit integer number, so debugging is made more difficult.

If you want a more human-readable value, I suggest using the ISO 8601 standard format.

Example: 2015-04-01T08:41:51+02:00

The Joda-Time library generates and parses such strings by default. If you do much work at all with date-time, I strongly suggest learning how to use Joda-Time rather than the troublesome and flawed java.util.Date/.Calendar classes bundled with Java (and Android). Joda-Time does work in Android.

UTC

Generally the best practice is to convert your date-time value to UTC when storing. At runtime, adjust to a desired time zone as expected by the user.

Example UTC value, where offset is set to zero.

2015-04-01T06:41:51+00:00

Upvotes: 0

krebernisak
krebernisak

Reputation: 970

You can save it in the SharedPreferences.

SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = spref.edit();
editor.putInt("lastStartTime", System.currentTimeMillis());
// Commit the edits!
editor.commit();

Upvotes: 3

osayilgan
osayilgan

Reputation: 5893

There is simpler way to do it. Use SharedPreferences.

Upvotes: 0

Related Questions