Java: Timezone Correction/Conversion with Daylight Savings Time settings


Have tried out a couple of code snippets to handle timezone corrections in Java code that’ll take into account Daylight Savings Time (DST) settings. None of them really suited our need; so I had to modify a particular code snippet which I found to be closer to what I was expecting.

Here’s the modified code:.

private static Date offsetTimeZone(Date date, String fromTZ, String toTZ){

// Construct FROM and TO TimeZone instances
TimeZone fromTimeZone = TimeZone.getTimeZone(fromTZ);
TimeZone toTimeZone = TimeZone.getTimeZone(toTZ);

// Get a Calendar instance using the default time zone and locale.
Calendar calendar = Calendar.getInstance();

// Set the calendar's time with the given date
calendar.setTimeZone(fromTimeZone);
calendar.setTime(date);

System.out.println("Input: " + calendar.getTime() + " in " + fromTimeZone.getDisplayName());

// FROM TimeZone to UTC
calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);

if (fromTimeZone.inDaylightTime(calendar.getTime())) {
calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
}

// UTC to TO TimeZone
calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());

if (toTimeZone.inDaylightTime(calendar.getTime())) {
calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
}

return calendar.getTime();

}

Sample Method Invocation: offsetTimeZone(new Date(), “Asia/Kolkata”, “US/Central”)

One good thing about this solution is that it’ll take care of DST settings in both the timezones (FROM and TO).

About these ads

4 comments

  1. This is useful but seems somewhere it went wrong.
    the above code gives wrong output in the below scenario : Could you please help in update this?

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

    public class Test {
    public static void main(String[] args) throws Exception {

    // Construct FROM and TO TimeZone instances
    TimeZone fromTimeZone = TimeZone.getTimeZone(“America/Chicago”);
    TimeZone toTimeZone = TimeZone.getDefault();

    // Get a Calendar instance using the default time zone and locale.
    Calendar calendar = Calendar.getInstance();

    // Set the calendar’s time with the given date
    calendar.setTimeZone(fromTimeZone);

    // calendar.setTime(new Date());
    calendar.set(2011, 4 – 1, 12 – 1); // APR 12 2011
    calendar.set(Calendar.HOUR, 12 – 1); // 11
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);

    calendar.set(Calendar.AM_PM, Calendar.PM);
    System.out.println(“Input : ” + calendar.getTime() + ” in ”
    + fromTimeZone.getDisplayName());

    // FROM TimeZone to UTC
    int rawOffset_from_UTC = fromTimeZone.getRawOffset();
    calendar.add(Calendar.MILLISECOND, rawOffset_from_UTC * -1);

    //System.out.println(rawOffset_from_UTC);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
    int dstOffset_from_UTC = calendar.getTimeZone().getDSTSavings();
    //System.out.println(dstOffset_from_UTC);
    calendar.add(Calendar.MILLISECOND, dstOffset_from_UTC * -1);
    }
    // UTC to TO TimeZone
    calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());

    if (toTimeZone.inDaylightTime(calendar.getTime())) {
    calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
    }
    System.out.println(“Output: ” + calendar.getTime() + ” in ”
    + toTimeZone.getDisplayName());
    }
    }

    ————————————————————–
    Actual Output :
    ————————————————————–
    Input : Tue Apr 12 00:00:00 EDT 2011 in Central Standard Time
    Output: Tue Apr 12 01:00:00 EDT 2011 in Eastern Standard Time
    ————————————————————–
    Expected output :
    ————————————————————–
    Input : Tue Apr 12 00:00:00 EDT 2011 in Central Standard Time
    Output: Tue Apr 13 01:00:00 EDT 2011 in Eastern Standard Time

    Differed at the out put date 13.

  2. Sorry about that. Its correct. I misunderstood and my brain is locked.

  3. that’s fine, it happens!! :)

  4. [...] It’s over the web. Could have googled. Anyways, here is a version for you (shamelessly picked and modified from here): [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: