12

I want to calculate the time difference between two dates (in the format "yyyyMMddHHmmss"). The basic idea is to first convert the string date into milliseconds and then get the time difference.

Calendar c1 = Calendar.getInstance();
c1.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse("20110327032913"));
System.out.println(c1.getTimeInMillis());
Calendar c2 = Calendar.getInstance();
c2.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse("20110327025913"));     
System.out.println(c2.getTimeInMillis());

Result:

1301189353000

1301191153000

Obviously, the first date is later than the second one, but its converted millisecond is smaller. Did I make any error on format?

jlordo
  • 37,490
  • 6
  • 58
  • 83
user2177467
  • 121
  • 1
  • 1
  • 4
  • 1
    It works fine for me, printing `1301221753000` and `1301219953000`. What timezone/locale are you running in? – FThompson Mar 16 '13 at 17:18
  • With my code run first value is grater than second. – Sachin Mar 16 '13 at 17:18
  • There's no problem with your code, it's printing something correct: http://ideone.com/EIS1wI – Alex Mar 16 '13 at 17:20
  • 1
    My guess is that a timezone change took place in your system's default locale, similar to [this question](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result). – FThompson Mar 16 '13 at 17:23
  • 1
    @Vulcan: Yes, timezone changes are common on March 27, 2011 in many countries, see [this link](http://www.timeanddate.com/time/dst/2011.html) – jlordo Mar 16 '13 at 17:24
  • As @Vulcan said, the `Locate` could be important in some cases, may like yours. This is an example: http://stackoverflow.com/questions/11510713/parsing-string-with-spaces-to-date-lead-to-parseexception/11510955#11510955 – manix Mar 16 '13 at 17:22

3 Answers3

8

The time difference between the two timestamps in ms is 30 minutes:

1301191153000 - 1301189353000 = 1800000ms = 30 min

Because of the DST changes on 27 march, the clock is being set forward at 2 AM from 2 AM to 3 AM, hence the timestamps:

20110327032913 => 2011-03-27 03:29:13

20110327025913 => 2011-03-27 02:59:13

are in fact interpreted as:

2011-03-27 03:29:13

2011-03-27 03:59:13 (+1 hour from original time)

Thus, the second timestamp comes later and when converted to ms it is bigger than the first one.

harpun
  • 4,022
  • 1
  • 36
  • 40
4

I bet you are in a locale where the time was changed from 2 o'clock (am) to 3 o'clock (am) for daylight saving time on March 27, 2011 (See this link)

Your first time is

2011 03 27 03 29 13
yyyy MM dd HH mm ss

and your second time is

2011 03 27 02 59 13 // does not exist because of time change
yyyy MM dd HH mm ss

so actually, your second time is:

           ,,
2011 03 27 03 59 13
yyyy MM dd HH mm ss

which is obviously 30 minutes later than your first one (and not 30 minutes earlier).

jlordo
  • 37,490
  • 6
  • 58
  • 83
2

Perhaps this code can help you:

        Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));            
        Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));       

        c1.setTime(sdf.parse("20110327032913"));
        System.out.println(c1.getTimeInMillis());

        c2.setTime(sdf.parse("20110327025913"));     
        System.out.println(c2.getTimeInMillis());
                    System.out.println((c1.getTimeInMillis()-c2.getTimeInMillis())/(1000*60)+ " minutes");

*The problem is that if your String is not in UTC you may never know if the time change was applied therefore an interval could be one hour off.

PbxMan
  • 7,525
  • 1
  • 36
  • 40