10.6: Fix errors with certain timezones in Java

Nov 03, '09 07:30:12AM

Contributed by: slyguy

Java on MacOS X 10.6 has some problems with certain timezones. This problem only affects Java programs, but will cause any Java programs to use incorrect dates. For example, any cities/towns/villages in southern Ontario (locations using "America/Toronto") will find their Java apps reporting an offset of five minutes behind GMT.

The solution is to explicitly specify an alternative location that doesn't have problems. For those in southern Ontario, Montreal works fine. To see if you have this problem, perform the following steps:

  1. Copy the following text to a file called tz.java in your home directory:
    class tz {
      public static void main(String[] args) {
        java.util.TimeZone tz = java.util.TimeZone.getDefault();
        System.out.println("Timezone offset from UTC reported as " +
    	(tz.getRawOffset() / 1000 / 60) + " minutes");
        if(tz.getRawOffset() % (15 * 60 * 1000) != 0) {
    	System.out.println("Warning: not a multiple of quarter-hours");
        }
        System.out.println(new java.util.Date());
        System.out.println(tz);
      }
    }
    
  2. javac tz.java

After compiling, run the program with java tz. Check that the information reported is correct for your area. Incorrect timezones seem to report an offset from UTC measured in minutes rather than hours or half-hours, and have an id with GMT with an offset rather than a city name. For example, an incorrect output:

Timezone offset from UTC reported as -5 minutes
Warning: not a multiple of quarter-hours
Tue Oct 20 16:02:32 GMT-00:05 2009
sun.util.calendar.ZoneInfo[id="GMT-00:05",offset=-300000,
  dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
A correct output should look something like:
Timezone offset from UTC reported as -300 minutes
Tue Oct 20 12:17:40 EDT 2009
sun.util.calendar.ZoneInfo[id="America/Montreal",offset=-18000000,
  dstSavings=3600000,useDaylight=true,transitions=231,
  lastRule=java.util.SimpleTimeZone[id=AmericaMontreal,offset=-18000000,
  dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,
  startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,
  endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
If the output doesn't look correct, then you'll need to set a different timezone location with System Preferences » Date & Time » Time Zone. Look at the files in /usr/share/zoneinfo for an appropriate city near you. You can access this directory from the Finder by using Go » Go to Folder, and then providing the path. I've logged this problem with Apple.

[robg adds: I tested this one, and it seems to work. My timezone offset was reported in minutes, but it's the correct number of minutes.]

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091020091912375