2

I would like to get the GMT equivalent using the TimeZone id Example:

Pacific/Fiji ==> should be GMT+13 because DST started on Sun, 21 Oct 2012. But instead, I get GMT+12.

Below is my code:

public static void main(String[] args){

    String m_utcDatePattern = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    String pnrCreation = "2012-12-10T01:14:22.000";

    Calendar calendar = convertDateStringToCalendar(pnrCreation, m_utcDatePattern);

    double offset = TimeZone.getTimeZone("Pacific/Fiji").getOffset(calendar.getTimeInMillis())/(60*60*1000.0);

    System.out.println("GMT+" + (int) offset);  //result is GMT+12, it should be GMT+13
}


//Date String to Calendar
public static Calendar convertDateStringToCalendar(String value, String fromPattern)     
{
       Calendar calendar;

       if (value == null)
       {
           return null;
       } 
       else 
       {
           DateFormat dateFormat = new SimpleDateFormat(fromPattern);
           Date date;
           try 
           {
               date = dateFormat.parse(value);
               calendar = Calendar.getInstance();
               calendar.setTime(date);
           } 
           catch (ParseException e) 
           {
               return null;
           }

           return calendar;
       }

}

Thanks in advance! :-)

Regards,
Racs

1 Answers1

3

Your code is a little bit broken at the moment anyway, but the fundamental problem is that you're using old time zone data.

I can't pinpoint the exact release where this changed right now, but in the TZDB 2012e release, Fiji had a rule of:

Rule    Fiji    2012    only    -   Jan 22  3:00    0   -

In the 2012h release, it had:

Rule    Fiji    2012    max -   Jan Sun>=18 3:00    0   -

(Along with other appropriate changes.)

I believe Fiji was going to give up daylight savings, but decided to keep them after all.

I don't know how easy it is to update the time zone database for "normal" Java - I suggest you use Joda Time instead, which allows you to use a locally-built version from up-to-date data. It's also a much cleaner API :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @matheszabi: getOffset does that already. That's why you have to specify a point in time. From the documentation: "If Daylight Saving Time is in effect at the specified date, the offset value is adjusted with the amount of daylight saving." – Jon Skeet Dec 11 '12 at 13:38
  • 1
    Thanks Jon! The TZ data in our server has been updated to JDK 6 with latest timezone patch 'tzdata2012j'. Thanks again for this swift solution. – vixenpixie14 Dec 18 '12 at 07:29
  • @Racs: Ooh, I wasn't aware that 2012j was out. Now I'll have to update Noda Time :) I'm glad that sorted it out for you. – Jon Skeet Dec 18 '12 at 09:10