Timezone behavior of Java

Jung-uk Kim jkim at FreeBSD.org
Tue May 25 19:02:06 UTC 2010


On Tuesday 25 May 2010 05:14 am, Osipov, Michael wrote:
> Hi folks,
>
> I came recently across a problem with the timezone settings in our
> Jetty on our machine.
>
> This is the ouput:
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ echo $LANG
> de_DE.UTF-8
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ date
> Di 25 Mai 2010 11:10:46 CEST
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ echo $TZ
> bash: TZ ist nicht gesetzt.
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ javac
> ShowTimezone.java
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ java
> ShowTimezone
> sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylig
>ht=false,transitions=0,lastRule=null]
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ export
> TZ="Europe/Berlin"
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ date
> Di 25 Mai 2010 11:11:25 CEST
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$ java
> ShowTimezone
> sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSav
>ings=3600000,useDaylight=true,transitions=143,lastRule=java.util.Sim
>pleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDa
>ylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDa
>yOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,end
>Day=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
> osipovmi at blnn719x:/.amd_mnt/blnn728x/home/osipovmi$
>
> Java source: System.out.println(TimeZone.getDefault());
>
> How can this be explained? Do I have to set the TZ variable
> globally? I assumed from date's behavior that /etc/localtime will
> suffice.

Works fine with OpenJDK6 b19:

hammer# cat TimeTest.java
import java.util.Date;
import java.util.TimeZone;

public class TimeTest {
        public static void main(String args[]) {
                long time = System.currentTimeMillis();
                String millis = Long.toString(time);
                Date date = new Date(time);
                System.out.println("Current time in milliseconds = " + millis + " => " + date.toString());
                System.out.println("Current time zone: " + TimeZone.getDefault().getID());
        }
}
hammer# javac -version
javac 1.6.0
hammer# java -version
openjdk version "1.6.0"
OpenJDK Runtime Environment (build 1.6.0-b19)
OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)
hammer# javac TimeTest.java
hammer# cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime
hammer# date
Wed May 26 03:50:39 KST 2010
hammer# echo $TZ
TZ: Undefined variable.
hammer# java TimeTest
Current time in milliseconds = 1274813492978 => Wed May 26 03:51:32 KST 2010
Current time zone: Asia/Seoul
hammer# cp /usr/share/zoneinfo/America/New_York /etc/localtime
hammer# date
Tue May 25 14:53:10 EDT 2010
hammer# java TimeTest
Current time in milliseconds = 1274813598908 => Tue May 25 14:53:18 EDT 2010
Current time zone: America/New_York
hammer# setenv TZ Asia/Seoul
hammer# echo $TZ
Asia/Seoul
hammer# java TimeTest
Current time in milliseconds = 1274813844489 => Wed May 26 03:57:24 KST 2010
Current time zone: Asia/Seoul
hammer# unsetenv TZ
hammer# echo $TZ
TZ: Undefined variable.
hammer# java TimeTest
Current time in milliseconds = 1274813869066 => Tue May 25 14:57:49 EDT 2010
Current time zone: America/New_York

This code example was taken from:

http://www.minaret.biz/tips/timezone.html

Jung-uk Kim


More information about the freebsd-java mailing list