3

I have used following code to convert UTC time to device current time zone time. It works fine on Indian Standard Time (IST). But it adds 1 hour additionally when device time zone is Sydney, Australia.

For example, I give UTC time like 2016-10-10T09:10:00 and it is converts as 8:10 PM, but actually I need 7:10 PM.What is the problem in my code?

public static String convertToLocalTimeFormat(String utcDateTimeString) {
    SimpleDateFormat simpleDateFormat;
    String formattedDateString = "";

    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());

    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date parsedDateObject;
    try {
        parsedDateObject = simpleDateFormat.parse(utcDateTimeString);
        SimpleDateFormat expectedSimpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.getDefault());
        formattedDateString = expectedSimpleDateFormat.format(parsedDateObject);
    } catch (ParseException e) {
        e.printStackTrace();
        return formattedDateString;
    }
    return formattedDateString;
}
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Ramprasad
  • 7,981
  • 20
  • 74
  • 135
  • daylight savings time? – Tibrogargan Sep 15 '16 at 07:35
  • @Tibrogargan Yes...Currently In Australia it is Australian Eastern Standard Time,But the given date is october...so after October 1st week there is Australian Eastern Daylight Time. So it adds 1 hour. But I need to show the same time what I enter now. – Ramprasad Sep 15 '16 at 07:38

1 Answers1

0

I would advise trying

System.out.println(expectedSimpleDateFormat.getTimeZone());

To see if all daylight saving schemes are correct on that device.

Light
  • 33
  • 9