标签云

微信群

扫码加入我们

WeChat QR Code

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?


It works fine for me, printing 1301221753000 and 1301219953000. What timezone/locale are you running in?

2018年08月16日23分45秒

With my code run first value is grater than second.

2018年08月16日23分45秒

There's no problem with your code, it's printing something correct: ideone.com/EIS1wI

2018年08月16日23分45秒

My guess is that a timezone change took place in your system's default locale, similar to this question.

2018年08月16日23分45秒

Vulcan: Yes, timezone changes are common on March 27, 2011 in many countries, see this link

2018年08月16日23分45秒

+1, same logic, you were faster.

2018年08月16日23分45秒

Thanks, this is exact the reason!

2018年08月16日23分45秒

Thanks! It works!

2018年08月16日23分45秒

You are welcome!

2018年08月16日23分45秒