Getting the correct date and time was always a problem but I could not expect it’s still a problem on today java and OS versions.

Trying to deploy a JBoss on various Linux and JBoss I could not notice that on each system the date in the log was different from the localtime. Initially I thought it’s just a JBoss configuration problem but a simple example shows it’s not:

import java.util.Date;

public class TestDate {	
public static void main(String[] args) {		
System.out.println("Current date is: " + new Date());	
}
}
<a href="mailto:len@black">len@black</a>:$ java TestDateCurrent date is: Tue Jul 03 14:55:03 GMT+02:00 2007

<a href="mailto:len@black">len@black</a>:$ dateTue Jul  3 15:55:13 EEST 2007

<a href="mailto:len@black">len@black</a>:$ java -versionjava version "1.5.0_11"Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)Java HotSpot(TM) Server VM (build 1.5.0_11-b03, mixed mode)

After running this on various systems from linux to solaris the results where completely haotic. It took some digging to find a solution for the problem but finally it was rather simple.

java -Duser.timezone=Europe/Bucharest TestDate
java -Duser.timezone=$(cat /etc/timezone) TestDate

Apparently java needs some confirmation of the timezone it’s in in order to display the date correctly.

Comments:

Marilen Corciovei -

Maybe you are lucky or it’s just java 1.6 which has this problem fixed.


Iulian -

ins@ovidiu-desktop:~/tests$ java -version
java version “1.6.0”
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
ins@ovidiu-desktop:~/tests$ java TestDate
Current date is: Wed Jul 04 16:00:50 EEST 2007
ins@ovidiu-desktop:~/tests$ date
Wed Jul 4 16:00:52 EEST 2007
ins@ovidiu-desktop:~/tests$ cat /etc/timezone
Europe/Bucharest
ins@ovidiu-desktop:~/tests$