-1

I know there are alot of these, but I can'e seem to find the magic string for this date format:

String textDate = "2018-04-25T18:23:57.556Z";

My code is:

String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
simpleDateFormat.parse(textDate)

What's weird is there is a "Z" in the date string itself, so I am not sure how the timezone works on this one.

If I change the date format to:

"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

It works, but I am not sure how to get the time zone then...

mmaceachran
  • 3,178
  • 7
  • 53
  • 102
  • 2
    FYI, the terribly troublesome old 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 Sep 03 '18 at 01:28
  • Always **search Stack Overflow** before posting. Most any date-time question has already been asked and answered. – Basil Bourque Sep 03 '18 at 01:30
  • `Z` = UTC ➡ [`java.time.Instant.parse`](https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#parse(java.lang.CharSequence)) – Basil Bourque Sep 03 '18 at 01:39
  • 1
    @BasilBourque, I did plenty of searches, for SimpleDateFormat, what I needed to search was for ISO 8601, which I did not know was the format. – mmaceachran Sep 03 '18 at 01:48
  • 1
    You are correct that you shouldn’t quote the `Z` in the format pattern string since this is the UTC offset and must be parsed as such to get the correct result. – Ole V.V. Sep 03 '18 at 03:05
  • You are correct that there are a lot of these. [Here’s just one.](https://stackoverflow.com/questions/3941357/string-to-date-time-object-in-android) If you tell us what trouble you had with those, I am sure we can and will guide you from there. – Ole V.V. Sep 03 '18 at 03:07
  • You may also want to check [this](https://stackoverflow.com/questions/43709133/how-to-get-start-and-end-range-from-list-of-timestamps), [this](https://stackoverflow.com/questions/46142198/string-date-into-epoch-time), [this](https://stackoverflow.com/questions/48666263/java-date-parsing-why-do-i-get-an-error) and/or [this](https://stackoverflow.com/questions/50368493/how-to-parse-yyyy-mm-ddthhmmss-sssxxx-date-format-to-simple-in-android). – Ole V.V. Sep 03 '18 at 03:09

1 Answers1

5

Z = UTC

The literal "Z" is actually part of the ISO 8601 datetime standard for UTC times. When "Z" (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time.

The java.time classes use the ISO 8601 standard formats by default when parsing/generating strings. The Instant class represents a moment in UTC, a perfect fit for your input string.

Instant instant = Instant.parse( "2018-04-25T18:23:57.556Z" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
alecc08
  • 66
  • 3