Published on Sep 10 2011 in Java

You can set timezone for many common programs like PHP or MySQL server. And of course you can do it with JVM. JVM Host clients can set timezone with our custom JVM control panel JCP. Timezone can also be set directly in shell variable or as Java command line parameter.

Let's create a simple program that will display date, time and timezone.

[~]# cat> GetCurrentDateTimeZone.java
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
    
public class GetCurrentDateTimeZone {
    public static void main(String[] args) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");
    Calendar cal = Calendar.getInstance();
    System.out.println(dateFormat.format(cal.getTime()));
     }
}

Now we compile it. $JAVA_HOME is set to our preferred JDK directory and $JAVA_HOME/bin is in our $PATH. This the default setting on JVM Host servers.

[~]# javac GetCurrentDateTimeZone.java

Now we can run the compiled class and perform some tests. First with current timezone.

[~]# java GetCurrentDateTimeZone
2011/07/28 15:08:46 CEST

With -Duser.timezone switch we can set timezone for our Java Virtual Machine. All applications and programs running on this JVM will use the timezone set this way.

[~]# java -Duser.timezone="America/New_York" GetCurrentDateTimeZone
2011/07/28 09:08:47 EDT

Another way of setting timezone not only for JVM but for all processes run in current shell is TZ environment variable. Lets set it to Indian Standard Time and check how JVM sees it.

[~]# export TZ="IST"
[~]# java GetCurrentDateTimeZone
2011/07/28 18:38:48 IST

Below you can see that -Duser.timezone overrides TZ environment variable.

[~]# echo $TZ
IST
[~]# java -Duser.timezone="America/Los_Angeles" GetCurrentDateTimeZone
2011/07/28 06:08:49 PDT

Note that JVM may be using old timezone database where the dates when summer time changes to winter time are not correct for current year. Thus a difference of 1 hour can occur for programs using a java instance with obsolete timezone database. You should stick with recent JDK versions to avoid this issue. You may also use TZUpdater from Oracle.

You can get list of all time zones and their properties with a simple program (compile it and run like above). Note that you can check if a zone supports Daylight Saving Time (DST) and if it is currently using it.

[~]# cat GetTimeZones.java 
import java.util.TimeZone;
import java.util.Date;
    
public class GetTimeZones {
    public static void main(String[] args) {
    
        Date today = new Date();
        String[] zoneIds = TimeZone.getAvailableIDs();

        for (int i=0; i<zoneIds.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);

        String shortName = tz.getDisplayName(tz.inDaylightTime(today), TimeZone.SHORT);
        String longName = tz.getDisplayName(tz.inDaylightTime(today), TimeZone.LONG);
    
        int rawOffset = tz.getRawOffset();
        int hour = rawOffset / (60*60*1000);
        int min = Math.abs(rawOffset / (60*1000)) % 60;
  
        boolean hasDST = tz.useDaylightTime();
        boolean inDST = tz.inDaylightTime(today);
    
        System.out.println(shortName+' '+longName+' '+hour+':'+min+' '+hasDST+' '+inDST);
        }
    }
}
    
[~]# java GetTimeZones
GMT-12:00 GMT-12:00 -12:0 false false
GMT-11:00 GMT-11:00 -11:0 false false
WST West Samoa Time -11:0 false false
...