Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 15
    This answer is working too hard. A java.util.Date by definition has no time zone. So no need for all that time zone related code: the LocalDateTime and ZoneId and atZone. This simple one-liner will do: java.util.Date date = Date.from( ZonedDateTime.parse( "2014-12-12T10:39:40Z" ).toInstant() ); Commented Dec 15, 2014 at 9:01
  • 7
    @BasilBourque This is unnecessarily complicated: Date.from(Instant.parse("2014-12-12T10:39:40Z" )); is enough. Commented Nov 13, 2015 at 19:07
  • 7
    @assylias you are correct but that will only work when the date string is UTC zone, ISO8601 allows any timezone... Commented Nov 13, 2015 at 19:13
  • 3
    @Adam My bad - I hadn't realised the question was more general than your example. As a side comment an OffsetDateTime would be enough to parse ISO8601 (which does not contain time zone information but only an offset). Commented Nov 13, 2015 at 19:18
  • 2
    @assylias Thanks for your comment about letting Instant do the parsing. While not sufficient for this particular Question, it is an important distinction worth pointing out. So I added a second example of code. Oops, just noticed this is not originally my Answer; I hope Adam approves. Commented Nov 13, 2015 at 22:26