Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Older dates are parsed as summer time, even if that is not true in Java

I have a problem displaying a number of dates that are stored as longs. I create the date objects with the constructor that takes the long argument, and then print the dates to a PDF file.

However, I have a problem with older dates, when running the program on Linux, compared to Windows.

Take this date: 25. april 1976 00:00:00 (long value: 199231200000L) for example. If I use a dateformater to display the date, it will display differently on Linux and Windows:

On Windows: 25. april 1976 00:00:00 CEST

On Linux: 24. april 1976 23:00:00 CET

The text rep. can be shown simply by running the following line:

DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL ).format( new Date( 199231200000L) )

I used Joda Time to get the datevalue for this test:

new org.joda.time.DateTime().withDate( 1976, 4, 25 ).withTime( 0, 0, 0, 0 ).toDate().getTime()

Why does Windows show the output as CEST, and Linux as CET?

like image 888
Anders K Avatar asked Nov 01 '22 02:11

Anders K


1 Answers

France has introduced summer time in 1976, but Denmark started DST in 1980. This explains the differences you observed so all is right except the fact that your system timezones are different. You should better use the same explicit timezone on both machines and not rely on the default timezone.

And by the way, if I look at the timestamps you use then I see no millisecond or just minute portion which makes me thinking if the intention was just to store a pure calendar date. If so then using timezones is in general dangerous as your problem clearly illustrates. Now Java-8 has the type LocalDate for this purpose - fortunately.

like image 82
Meno Hochschild Avatar answered Nov 09 '22 15:11

Meno Hochschild