0

I am using the following method to get a difference between two time stamps.

public static long tokenTimeDuration(String loginTime, String expiryTime){
    long diff = 0;

    Log.i("du_loginTimeFromServer",loginTime);
    Log.i("du_expiryTimeFromServer",expiryTime);
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss a z yyyy");
    format.setLenient(false);
    Date loginDate = null;
    Date expiryDate = null;

    try {
        loginDate = (Date)format.parse(loginTime);
        Log.i("du_loginTimeAfterParsing",format.format(loginDate));
        expiryDate = (Date)format.parse(expiryTime);
        Log.i("du_expiryTimeAfterParsing",format.format(expiryDate));

        Log.i("du_loginTimeInMilliseconds",loginDate.getTime()+"");
        Log.i("du_expiryTimeInMilliseconds",expiryDate.getTime()+"");

         //in milliseconds
        diff = expiryDate.getTime() - loginDate.getTime();
        Log.i("du_tokenExpiryDuration", diff + "");

    } catch (Exception e) {
        Log.i("duEx", e.getMessage());
    }

    return diff;
}

But while trying to use the method, I'm getting a wrong conversion of dates. And log output for the same as below:

12-05 19:30:45.802: I/du_loginTimeFromServer(16894): Fri Dec 05 14:00:44 PM UTC 2014
12-05 19:30:45.802: I/du_expiryTimeFromServer(16894): Fri Dec 05 17:20:44 PM UTC 2014
12-05 19:30:45.822: I/du_loginTimeAfterParsing(16894): Fri Dec 05 05:30:44 AM IST 2014
12-05 19:30:45.824: I/du_expiryTimeAfterParsing(16894): Fri Dec 05 05:50:44 AM IST 2014
12-05 19:30:45.824: I/du_loginTimeInMilliseconds(16894): 1417737644000
12-05 19:30:45.824: I/du_expiryTimeInMilliseconds(16894): 1417738844000
12-05 19:30:45.824: I/du_tokenExpiryDuration(16894): 1200000

Now sometimes it gives a negative difference even if the expiry time is 4 hours ahead of the login time. Please help me find where am I going wrong.

Update: If I add

    format.setTimeZone(TimeZone.getTimeZone("UTC"));

The logcat says:

12-05 22:05:59.903: I/du_loginTimeFromServer(23574): Fri Dec 05 16:35:58 PM UTC 2014
12-05 22:05:59.903: I/du_expiryTimeFromServer(23574): Fri Dec 05 19:55:58 PM UTC 2014
12-05 22:05:59.915: I/du_loginTimeAfterParsing(23574): Fri Dec 05 00:35:58 AM UTC 2014
12-05 22:05:59.918: I/du_expiryTimeAfterParsing(23574): Fri Dec 05 00:55:58 AM UTC 2014
12-05 22:05:59.918: I/du_loginTimeInMilliseconds(23574): 1417739758000
12-05 22:05:59.919: I/du_expiryTimeInMilliseconds(23574): 1417740958000
12-05 22:05:59.919: I/du_tokenExpiryDuration(23574): 1200000

Still not correct.

Akshay Sethi
  • 803
  • 3
  • 13
  • 22

3 Answers3

1

This issue has been resolved by using joda time

The code snippet for the same is:

public static long tokenTimeDuration(String loginTime, String expiryTime){
     DateTimeFormatter FMT = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss a z yyyy");
        final DateTime dt1 = new DateTime(FMT.parseDateTime(loginTime).withZone(DateTimeZone.getDefault()));
        Log.i("du_loginTimeAfterParsing",dt1.toString());

        final DateTime dt2 = new DateTime(FMT.parseDateTime(expiryTime).withZone(DateTimeZone.getDefault()));
        Log.i("du_expiryTimeAfterParsing",dt2.toString());

        long diff = dt2.getMillis() - dt1.getMillis();

        Log.i("du_tokenExpiryDuration", diff + "");    
        return diff;
}

And the logcat output now says:

12-06 06:48:22.073: I/du_loginTimeAfterParsing(10440): 2014-12-06T06:48:22.000+05:30
12-06 06:48:22.077: I/du_expiryTimeAfterParsing(10440): 2014-12-06T10:08:22.000+05:30
12-06 06:48:22.078: I/du_tokenExpiryDuration(10440): 12000000

The solution has been added so that others can get benefited from the same.

Akshay Sethi
  • 803
  • 3
  • 13
  • 22
0

Time zones are not aligned, you have UTC for server, and IST after parsing. Try adding something like this

format.setTimeZone(TimeZone.getTimeZone("UTC"));
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
0

Just a shot in the dark (although I did solve a similar issue here with this) but try explicitly adding your Locale to your SimpleDateFormat:

DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss a z yyyy", Locale.US);//Or whatever your desired Locale is

Also, I don't think it's necessary to cast loginDate and expiryDate to Date, as parse already returns a Date object.

Community
  • 1
  • 1
Alexander Kohler
  • 1,907
  • 16
  • 23