0

I have a given string 06/17/2008T13:53:23Z, I want to convert that to EDT time zone. So my output will be 6/17/08 9:53 AM EDT.

Below is my Java Code, it is giving wrong output:

    SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy'T'hh:mm:ss'Z'");
    SimpleDateFormat format2 = new SimpleDateFormat("M/d/yy hh:mm a");
    format1.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = format1.parse("06/17/2008T13:53:23Z");
    System.out.println(date);
    format2.setTimeZone(TimeZone.getTimeZone("EDT"));
    System.out.println(format2.format(date));

It gives output as :

Tue Jun 17 19:23:23 IST 2008
6/17/08 01:53 PM

Here the output time as 01:53 PM instead of 9:53 AM EDT

How to fix this issue?

learner
  • 6,062
  • 14
  • 79
  • 139

1 Answers1

2

Your parsing pattern uses hh (which is for 12-hour format) whereas the time in your date-time string is in 24-hour format for which you need to use HH. Secondly, you should avoid using the three-letter time-zone name. As you have already expected, EDT has a Zone-Offset of -4 hours and you can use this as GMT-4 with SimpleDateFormat.

While a Zone-Offset is expressed in terms of numbers (hours, minutes or seconds), a timezone is expressed as a string representing the name (e.g. America/New_York) of the timezone. The relation between timezone and Zone-Offset is many-to-one i.e. many timezones can have the same Zone-Offset.

Note that java.util date-time classes are outdated and error-prone and so is their formatting API, SimpleDateFormat. I suggest you should stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

If you are doing it for your Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Using the modern date-time API:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String str = "06/17/2008T13:53:23Z";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/u'T'H:m:sz");
        ZonedDateTime zdt = ZonedDateTime.parse(str, formatter);

        // Convert to Eastern Time
        ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
        // ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneOffset.ofHours(-4));// Or this
        
        // Print in default format i.e. ZonedDateTime#toString
        System.out.println(zdtET);

        // Print in custom formats
        System.out.println(zdtET.format(DateTimeFormatter.ofPattern("MM/dd/uuuu'T'HH:mm:ss zzzz")));
        System.out.println(zdtET.format(DateTimeFormatter.ofPattern("M/d/uu hh:mm:ss a")));
    }
}

Output:

2008-06-17T09:53:23-04:00[America/New_York]
06/17/2008T09:53:23 Eastern Daylight Time
6/17/08 09:53:23 am

Note: If you use ZoneOffset.ofHours(-4) [commented in the code above], you can not get the name of timezone (e.g. America/New_York) in the output because, as explained earlier, many timezones can have the same Zone-Offset and there is no default timezone for a Zone-Offset.

Using the legacy API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        String str = "06/17/2008T13:53:23Z";
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        Date date = formatter.parse(str);

        // Convert to Eastern Time
        SimpleDateFormat sdfOutput = new SimpleDateFormat("M/d/yy hh:mm a");
        sdfOutput.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        // sdfOutput.setTimeZone(TimeZone.getTimeZone("GMT-4"));// Or this

        System.out.println(sdfOutput.format(date));
    }
}

Output:

6/17/08 09:53 am
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • If North American Eastern Daylight Time is intended (other interpretations of EDT exist), the most correct and preferred will be America/Toronto or America/New_York, not GMT-4. – Ole V.V. Oct 22 '20 at 21:15
  • I agree with the comment above. Time/daylight savings time is not uniform, you can't just assume an offset from GMT. – Rob C Oct 28 '20 at 07:50