Time interval longer than

I have: startDate = 03.24.2013 21:01:20 endDate = 03.24.2013 21:01:40

I do in my code

Date d = new Date(endDate.getTime()-startDate.getTime) 

and d = 01/01/1970 02:00:20

If 2 hours is required, the interval should be 20 seconds, but I get 2 hours and 20 seconds. Why is this happening? What to do about it?

+4
source share
2 answers

What you do is new Date("difference between two dates") and time 01/01/1970. But what temporary displacement was put into effect on 01.01.1970 in your region? I guess +02 hours.

You should also print the result of new Date(0L) and compare the difference. Or make sure the time zone is set to UTC when you display the date.

+3
source

I don’t know what you did there, but I get the correct result:

http://ideone.com/dTbugh

  DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); Date startDate = df.parse("24.03.2013 21:01:20"); Date endDate = df.parse("24.03.2013 21:01:40"); Date d = new Date(endDate.getTime()-startDate.getTime()); System.out.print(d); 

Exit: Thu 01 January 00:00:20 GMT 1970

0
source

All Articles