I am facing a problem that I am saving the current device time in shared preferences behind a button click event. E.g: 11:05 am for the date(27-08-2015). After that when the user come to the same activity I have to change the layout file for the next 24 hours till (11:05 am for the date 28-08-2015) as per the project requirements. But I am facing the problem that how can I get that if 24 hours has been passed or not from the time of button click which was 11:05 am in this case. Any sort of help will be highly appreciated. Thank You.
-
1just save time in shared preference and when you came back at that time compare saved time with current time.Simple..!! – AndiGeeky Aug 27 '15 at 08:09
-
2The answer really depends on whether you mean "tomorrow after 11:05am local time" or "24 hours later"? – Andreas Aug 27 '15 at 08:16
-
2FYI: In my previous comment "local time" is affected both by daylight savings time, and by traveling (car, plane, boat, bicycle, ...) to a different time zone. – Andreas Aug 27 '15 at 08:28
6 Answers
If you want "24 hours later", use pure UTC time. Simplest is to use the raw millis.
// Save current time
long savedMillis = System.currentTimeMillis();
// Check time elapsed
if (System.currentTimeMillis() >= savedMillis + 24 * 60 * 60 * 1000) {
// time has elapsed
}
If you want "after same time of day tomorrow", you have to either remember the timezone, or simply convert to un-zoned text.
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
// Save current time
String savedTime = df.format(new Date()); // Applies local time zone 1
// Check time elapsed
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(savedTime)); // Applies local time zone 2
cal.add(Calendar.DAY_OF_MONTH, 1); // Adjusts for DST in local time zone 2
if (System.currentTimeMillis() >= cal.getTimeInMillis()) {
// time has elapsed
}
Explanation
new Date()
returns an instant in UTC time.
df.format
returns text representation of that instant in the current default timezone, i.e. the timezone in effect for the JVM at the time the call is made (or rather when the SimpleDateFormat was created).
Later on, df.parse
will parse that text back into an instant in UTC time, again using the current default timezone, but that may be a different timezone than used when df.format
was called.
cal.setTime
updates the Calendar
to the instant in UTC time.
cal.add
updates the Calendar
to the same time 1 day later, adjusting for DST in the current default timezone, if needed.
cal.getTimeInMillis()
is the UTC millis of the same time of day, 1 day after the saved time, in local time zone.

- 154,647
- 11
- 152
- 247
You usually compare times in milliseconds. Just check if the difference between the two times is bigger than 24*60*60*1000
If you want a safer comparison, use Androids Calendar
. It checks for several things like timezones etc.

- 7,690
- 3
- 24
- 62
-
2
-
1@RobAu Correct, it becomes more complicated if he just flew from New York to LA. Is it "tomorrow after 11:05am local time" or is it "24 hours later"? – Andreas Aug 27 '15 at 08:13
whenever the user returns just compare the current time with the one you saved in shared preferences. if the difference is 24h or more do whatever you wanted to do.
it also would be good to know in which environment / programming language you try to do this

- 2,513
- 2
- 21
- 60
-
-
in that case this post is probably helpful: comparison of time inside java: http://stackoverflow.com/questions/2309558/time-comparison – Cribber Aug 27 '15 at 08:21
-
A pure time solution will not work. If stored time is "11:05" (without a date) and next run is "10:45", is that tomorrow (<24 hrs) or some later date (>24 hrs). Similar is "11:48" later today, or some later date? – Andreas Aug 27 '15 at 08:25
-
in that case you have to compare the date first and then the time if necessary. I dont see the problem? – Cribber Aug 27 '15 at 08:30
You could use something like this:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
-http://developer.android.com/reference/android/os/CountDownTimer.html
But I think Mamata is right, especially if it is going to be every day.
Have a boolean value hasDayPassed that is set false when a new currentTime is set and then when the currentTime is compared to the newer time is true if they match.
Good luck

- 130
- 14
-
This is for 30 seconds. 24 hours include a lot of things like System reboot, App force closed because low battery etc. So ideal way is to save time in preferences. It also reduces battery drain. – Seshu Vinay Aug 27 '15 at 08:27
There are two cases:
- the first is no timezone considered: here you can do in several ways,
the first i think at is:
//DateFormat is used formatting the Date with the sintax you like
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date today = Calendar.getInstance().getTime();
//save date in SharedPreferences using
String dateString = df.format(today);
sp.putString("date", dateString);
and when you have to get it back:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date;
if(preferences.contains("date")){
String dateString = preferences.getString("date", "");
if(dateString != ""){
date = df.parse(dateString);
if(new Date() >= date.addDays(1)){
//a day is passed
} else{
//not passed
}
}else{
//case date wrongly stored
}
} else{
//no date stored
}
- if you have to consider timezones, add in sharedPref an int representing the +/- hours in timezone, using
TimeZone
class. Than when you get it back you have to compare the two timezones and add/subtract the difference from the new Date.

- 5,305
- 4
- 27
- 66
Save time in preferences:
Editor editor=mSharedPreferences.edit(); //mSharedPreferences is instance of your SharedPreferences
editor.putLong("time",System.currentTimeMillis());
editor.commit();
Check whenever you want to:
if(System.currentTimeMillis()-mSharedPreferences.getLong("time",0)<24*60*60*1000){
//with in a day
}else{
//greater than a day
}
P.S.: It doesn't consider timezone though.

- 13,560
- 9
- 60
- 109