Older dates are parsed as daylight saving time, even if this is not true in Java

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

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

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

Windows: April 25, 1976 00:00:00 CEST

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

Text rep. you can simply start by running the following line:

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

I used Joda Time to get the date value for this test:

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

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

+5
source share
1 answer

France introduced daylight saving time in 1976, but Denmark began DST in 1980. This explains the differences that you observed, so everything is correct, except that your system time zones are different. You are better off using the same explicit time zone on both machines and not rely on the default time zone.

And by the way, if I look at the timestamps that you use, I don’t see a single millisecond or minute portion, which makes me think if there was an intention to keep a clean calendar date. If so, using time zones is generally dangerous, as your problem clearly illustrates. Fortunately, Java-8 is of type LocalDate for this purpose.

+2
source

All Articles