user9451426
user9451426

Reputation: 21

Convert UTC to EST timezone

I have to convert UTC timestamp data to EST timezone. Below code is working fine when timezone difference is -5Hrs but when I give UTC time like - 2018-04-15T21:27:31.000Z then it outputs as 2018-04-15 16:27:31 -0500 which is not correct. Output should be 2018-04-15 17:27:31 -0400. It always subtract -5hrs.

DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat estFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
estFormat.setTimeZone(TimeZone.getTimeZone("EST"));

try {
    String date1 = estFormat.format(utcFormat.parse("2018-04-15T21:27:31.000Z"));

    System.out.println("est time : "+date1);

} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Upvotes: 0

Views: 1431

Answers (1)

kumesana
kumesana

Reputation: 2490

You seem to expect the time zone shorthand named "EST" to obey the daylight saving time change rules. It doesn't. "EST" is the name for a time zone which is GMT-5 at any time of the year.

To get time zone definitions that obey daylight saving time rules as expected, you'll be better off using the name of the main cities that use these time zones.

In your case, try "America/New_York"

Upvotes: 1

Related Questions