Search code examples
dategrailssystem

grails System.currentTimeMillis() differ from System-Date


I am running centOs with Grails on Tomcat and when i type date in ssh remote shell

it prints

Mo 2. Jun 18:18:57 CEST 2014

in the App I tried the following line of code

answer.insertDate = new Date(System.currentTimeMillis())

which returns the date two hours before so

16:18:57

so anybody can explain how I can get the correct system time?


Solution

  • CEST is 2 hours ahead of UTC.

    I believe when you run from shell it uses your system timezone (CEST) but when you run the app (which is programmed to use UTC by default) is 2 hours behind.

    Set default timeZone (in BootStrap.groovy) to CEST as below if you want CEST timeZone to be used in the app

    TimeZone.setDefault( TimeZone.getTimeZone( 'Europe/Brussels' ) )
    

    Also note, above changes the timeZone for the app altogether (you would see logs with CEST timestamp as well), so choose this setting wisely. If the app expects UTC to be the default timeZone then you can stick to the app and do not alter the default timeZone. :)