0

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

}

akhilgm
  • 39
  • 1
  • 5

3 Answers3

1

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.

Kshitij
  • 361
  • 1
  • 9
  • public void convert(Date dt){ Calendar cal = Calendar.getInstance(); cal.setTime(dt); // Geting time in IST long millis = cal.getTimeInMillis(); GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/Chicago"),Locale.US); calendar.setTimeInMillis(millis); Date date = calendar.getTime(); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));} This is what i tried. But i got the output as the same datetime which i have given as the input... !!! – akhilgm Mar 14 '13 at 13:23
  • @akhilgm89, you are getting the same date string because you are using `SimpleDateFormat` instance with default time zone, which will be your system's time zone. A `Date` instance is just a long value denoting milliseconds since epoch; it does not contain time zone information. When you create string representations, you need to specify the timezone to use. I've edited my post to explain this with code. Have a look. – Kshitij Mar 15 '13 at 05:49
0

java.util.GregorianCalendar allows you create dates with timezones. Unfortunately, addition and subtraction suck from there. (How do you subtract Dates in Java?)

Since you're converting between two timezones, you can also make use of java.util.TimeZone and use the difference of tz1.getOffset(date) - tz2.getOffset(date). Mind the ordering when doing subtraction.

Community
  • 1
  • 1
Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
0

Joda-Time

Using Joda-Time makes this much easier. Or try the new java.time package in Java 8.

Here is some example code using Joda-Time 2.3. Search StackOverflow for many more examples.

India time…

DateTimeZone timeZone_India = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( date, timeZone_India );

Adjusting the same moment for display as New York time…

DateTimeZone timeZone_NewYork = DateTimeZone.forID( "America/New_York" );
DateTime dateTimeNewYork = dateTimeIndia.withZone( timeZone_NewYork ); // Same moment, different wall-clock time.

Still the same moment, but in UTC (no time zone offset).

DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );

Use Proper Time Zone Names

Avoid using 3-4 letter time zone codes. They are neither standardized nor unique. Your IST for example can mean either Irish Standard Time or India Standard Time. Use proper time zone names.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154