java timezones and summer- wintertime
java, timezone
Solution
The time in Java is just a simple long value (milliseconds since 1970) without any information about the time zone. The `java.util.Date` and `java.sql.Date` also store the date/time internally as milliseconds since 1970, but with UTC time zone.
The time zone comes into play, when you format a date/time for output or when you parse a date/time from a string. The time zone information you set via `-Duser.timezone` will be used at that time.
So it should work and a little test also shows it:
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(2013, 2, 30, 23, 0, 0);
long start = c.getTimeInMillis();
long oneHour = 1000 * 60 * 60;
long t = start;
for (long i = 0; i < 5; i++) {
System.out.println(new Date(t));
t = t + oneHour;
}
}
using `-Duser.timezone=GMT` it will print: (no switch)
Sat Mar 30 23:00:00 GMT 2013
Sun Mar 31 00:00:00 GMT 2013
Sun Mar 31 01:00:00 GMT 2013
Sun Mar 31 02:00:00 GMT 2013
Sun Mar 31 03:00:00 GMT 2013
using `-Duser.timezone=CET` it will print: (switch at 2am)
Sat Mar 30 23:00:00 CET 2013
Sun Mar 31 00:00:00 CET 2013
Sun Mar 31 01:00:00 CET 2013
Sun Mar 31 03:00:00 CEST 2013
Sun Mar 31 04:00:00 CEST 2013
using `-Duser.timezone=EET` it will print: (Eastern European Time, one hour after CET)
Sat Mar 30 23:00:00 EET 2013
Sun Mar 31 00:00:00 EET 2013
Sun Mar 31 01:00:00 EET 2013
Sun Mar 31 02:00:00 EET 2013
Sun Mar 31 04:00:00 EEST 2013
Problem
We have a java programm which uses the -Duser.timezone parameter at startup: -Duser.timezone="CET" This is neccessary because we can't rely on the servers internal clock (admins playing around, setting wrong timezones and so on, don't ask :-( ) and it is important that the timezone of the frontend matches the backend. As this program is not restarted regularly (it is a server) I was wondering what happens when we switch from winter- to summertime? Does it switch automatically or do we have to restart the server? Thanks and regards, Alex Edit: It might be possible that java determines the correct timezone with ervery call to a function which uses dates. But it might also be possible that java determines the correct timezone once at startup.