Search code examples
javatimezonedst

Convert IST to US timezones considering the daylight saving time in java


I have a date-time in IST. I want to convert it to US timezones based on input considering the daylight saving time, if there is daylight saving time for the given date-time in java. This is what i tried

function convert(Date dt,int toTimeZoneId){
Calendar cal = Calendar.getInstance();
cal.setTime(dt); // Geting time in IST

//Converted to GMT and set in cal

switch(toTimeZoneId){
    case 1: tzTarget = TimeZone.getTimeZone("America/Adak"); 
        offset = -10;
        break;
    case 2: tzTarget = TimeZone.getTimeZone("America/Anchorage"); 
        offset = -9;
        break;
    case 3: tzTarget = TimeZone.getTimeZone("America/Los_Angeles"); 
        offset = -8;
        break;

    case 4: tzTarget = TimeZone.getTimeZone("America/Denver");
        offset = -7;
        break;
    case 5: tzTarget = TimeZone.getTimeZone("America/Chicago");
        offset = -6;
        break;
    case 6: tzTarget = TimeZone.getTimeZone("America/New_York");
        offset = -5;
        break;
}
//converting from GMT to US timezones based on offset and dst
cal.setTimeZone(tzTarget);
dst = tzTarget.getDSTSavings();
dst = dst/3600000;
offset = offset + dst;
cal.add(Calendar.HOUR, offset);

Date date = cal.getTime();

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

}


Solution

  • To just convert a given date to different time zones, you need to create the date formatter with appropriate time zone. A date instance is just a long value relative to epoch; it doesn't have time zone information. So we aren't converting it to a different time zone, we are just representing it in different time zones. That is why we need time zone information when we want to create a string representation of the date instance.

    Here's some code to illustrate the above. I've just added the time zone to your date format string to make things clear.

    /*
     * Converts a specified time to different time zones
     */
    public void convert(Date dt) {
        // This prints: Date with default formatter: 2013-03-14 22:00:12 PDT
        // As my machine is in PDT time zone
        System.out.println("Date with default formatter: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(dt));
    
        // This prints: Date with IST time zone formatter: 2013-03-15 10:30:12 GMT+05:30
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        TimeZone tz = TimeZone.getTimeZone("GMT+0530");
        sdf.setTimeZone(tz);
        String dateIST = sdf.format(dt);
        System.out.println("Date with IST time zone formatter: " + dateIST);
    
        // This prints: Date CST time zone formatter: 2013-03-15 00:00:12 CDT        
        tz = TimeZone.getTimeZone("CST");
        sdf.setTimeZone(tz);
        System.out.println("Date CST time zone formatter: " + sdf.format(dt));
    }
    

    I think this is what you are trying to do - convert a given time to different time zones. To do that I don't think you need to add/subtract any offset, as you just want the same time represented in a different time zone and the TimeZone instance should be able to take care of that during formatting.

    As for daylight saving, the TimeZone should be able to take care of that as well. If you notice in my example code, I've used CST to create TimeZone instance and CST is "GMT -06 hours". But the output it gives is in CDT, which is "GMT -05 hours", because this time zone instance uses daylight saving. So by using the appropriate time zone you should be able to handle daylight saving as well.