0

Getting following exception while parsing UTC date time to EST local time.

Exception:

Stacktrace:] with root cause
 java.text.ParseException: Unparseable date: "2016-09-09T03:00:29Z"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at com.study.crud.util.GenericUtils.convertUTCDateToEST(GenericUtils.java:55)

GenericUtils.java

public static String convertUTCDateToEST(String utcDate) throws ParseException {
        SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        inFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date aDate = inFormat.parse(utcDate);

        DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:MI:SS");
        outFormat.setTimeZone(TimeZone.getTimeZone("EST"));
        String estDate = outFormat.format(aDate);

        return estDate;
    }

Found some similar stuff on SO here java.text.ParseException: Unparseable date "yyyy-MM-dd'T'HH:mm:ss.SSSZ" - SimpleDateFormat and tried solutions proposed there but did not work.

Community
  • 1
  • 1
Nital
  • 5,292
  • 20
  • 88
  • 174
  • Use `mm:ss`, not `MI:SS` in your output format. – Dawood ibn Kareem Sep 09 '16 at 03:38
  • Reference this [http://stackoverflow.com/questions/19375357/java-convert-gmt-utc-to-local-time-doesnt-work-as-expected][1] – zpc Sep 09 '16 at 03:45
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 06 '19 at 06:02

2 Answers2

1

There is a error in the input format. You have specified milli seconds as an input to your format .SSS and then the zone Z.

You have however not passed the milli second option. The following format works

SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Ramachandran.A.G
  • 4,110
  • 1
  • 10
  • 20
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 06 '19 at 06:02
0

It is because that the millisecond part of the time is absent from your input:

"2016-09-09T03:00:29Z" 
                    ^ unmatched place

You can either revise the input by adding the missing part(2016-09-09T03:00:29.000Z) or change your format pattern accordingly(yyyy-MM-dd'T'HH:mm:ss'Z').

Also, there's a typo in your output format (the minute part of the time):

"yyyy-MM-dd HH:MI:SS"

It should be replaced with yyyy-MM-dd HH:mm:ss.

Jerry Chin
  • 639
  • 1
  • 7
  • 23