Skip to content Skip to sidebar Skip to footer

How To Count That 24 Hours Passed Or Not

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 c

Solution 1:

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.

SimpleDateFormatdf=newSimpleDateFormat("yyyyMMddHHmmss");

// Save current timeStringsavedTime= df.format(newDate()); // Applies local time zone 1// Check time elapsedCalendarcal= 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 2if (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.

Solution 2:

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.

Solution 3:

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

Solution 4:

You could use something like this:

new CountDownTimer(30000, 1000) {

     publicvoidonTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     publicvoidonFinish() {
         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

Solution 5:

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 likeDateFormatdf=newSimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Datetoday= Calendar.getInstance().getTime();
//save date in SharedPreferences usingStringdateString= 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.

Post a Comment for "How To Count That 24 Hours Passed Or Not"