Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awkward Java Date creation behaviour

I've just come upon a very strange behaviour of Java’s Date class when I try to create two dates consequently:

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

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

The output is respectively:

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

Has anyone seen something like that? Both Dates are initialized in the same manner, but when printed, the first is shown in BST and the latter in GMT? How come?

I tried to find explanation about that but I didn’t.

like image 825
shadrik Avatar asked Feb 22 '26 17:02

shadrik


1 Answers

This is documented behaviour.

From Date.toString():

Converts this Date object to a String of the form:

 dow mon dd hh:mm:ss zzz yyyy

zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.

You are using a locale that uses British Summer Time and creating a date where a day-light-saving rule applies. This would be the expected form of the date at that time to a local user.

like image 168
McDowell Avatar answered Feb 25 '26 08:02

McDowell