Question

I have a program that uses java.util.TimeZone to produce the GMT offsets for various timezones. Recently it was pointed out that one of the timezones was off by an hour, Argentina Buenos Aires.

I wrote a simple java program to see what the java TimeZone class was giving as the offset. It turns out it was giving the wrong value for Buenos Aires but all the other timezones (that we use) were ok. I think the problem might be the java version on the machine that runs the code. One machine (v1.6.0_22) gives the correct value the other (v1.5.0) gives the incorrect value. Has anyone else had an issue like this, or have a different explination for the differences in offset values? Here is the test program I used:

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TZTest {
  public static void main(String[] args) {
    System.out.println("Java version = "+ System.getProperty("java.version"));

    Calendar now = Calendar.getInstance();
    TimeZone timeZone = now.getTimeZone();

    System.out.println("Current System Time is: " + new Date(System.currentTimeMillis()));
    System.out.println("Current Calendar Time is: " + new Date(now.getTimeInMillis()));
    System.out.println("Current TimeZone is: \"" + timeZone.getDisplayName() + "\": ID = " + timeZone.getID());

    //One ID for the timezone in question
    String id = "America/Argentina/Buenos_Aires";
    TimeZone tz = TimeZone.getTimeZone(id);
    System.out.println("\ntimeZone.getTimeZone(ID) using ID = \""+ id + "\" returns\n" + TimeZone.getTimeZone(id));
    System.out.println("Offset in hours: "+(tz.getOffset(System.currentTimeMillis())/new Double(3600000)));

    //A second ID for the timezone in question
    String id2 = "America/Buenos_Aires";
    TimeZone tz2 = TimeZone.getTimeZone(id2);
    System.out.println("\ntimeZone.getTimeZone(ID2) using ID2 = \""+ id2 + "\" returns\n" + TimeZone.getTimeZone(id2));
    System.out.println("Offset in hours: "+(tz2.getOffset(System.currentTimeMillis())/new Double(3600000)));
  }
}
Was it helpful?

Solution

each java release includes its own timezone database (this is documented in the release notes for each version). 1.5.0 is very old, and presumably has out of date or incorrect information for the timezone you are looking at.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top