Awkward Java Date Creation Behavior

I just came across a very strange behavior of the Java Date class when I try to create two dates:

Date startDate = new Date(1282863600000L);
System.out.println(startDate);

Date endDate = new Date(1321919999000L);
System.out.println(endDate);

Result:

Fri Aug 27 00:00:00 BST 2010
Mon Nov 21 23:59:59 GMT 2011

Has anyone seen something like this? Both dates are initialized in the same way, but when printed, is the first displayed in BST and the second in GMT?

I tried to find an explanation for this, but I did not. Can anybody help me?

Thanks in advance!

+3
source share
3 answers

This is documented behavior.

From Date.toString():

Converts this Date object to a String of the form:

 dow mon dd hh:mm:ss zzz yyyy

zzz ( ). , . , zzz , .. .

, , , . .

+5

Fri Aug 27 01:00:00 CEST 2010
Tue Nov 22 00:59:59 CET 2011

Java, , .

, CEST - , CET - (.. ).

, , (en_GB ), .

, , , - . Java /.

+1

long :

Date startDate1 = new Date(1284245999999L);
Date startDate2 = new Date(1284246000000L);
System.out.println(startDate1);
System.out.println(startDate2);

Date endDate = new Date(1321919999000L);
System.out.println(endDate);

:

Sun Sep 12 01:59:59 IDT 2010
Sun Sep 12 01:00:00 IST 2010 <-- Long value is greater, but due to DST changes, actual time is one hour earlier
Tue Nov 22 01:59:59 IST 2011

Please note that incrementing long 1from from 1284245999999Lto 1284246000000Lreturns us "back in time" due to the transition from standard time to daylight saving time.
This is how Java time calculation behaves - the number of milliseconds has not changed since January 1, 1970, but the time it represents is based on the time zone.

+1
source

All Articles