vixenpixie14
vixenpixie14

Reputation: 47

How to get the GMT equivalent using TimeZone ID

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

Upvotes: 2

Views: 767

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502076

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 :)

Upvotes: 3

Related Questions