0

How do I parse YYYY-MM-DD dates in modern Java?

I have a Java String of a standard ISO 8601 date in the YYYY-MM-DD format, such as 2016-03-21.

How can I parse this into the modern Date-Time types in Java, the java.time classes?

Note: This is intended to be a canonical post for a common question.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I posted this Question and Answer as a reference for this specific case that requires no formatting pattern to be defined. Comes up frequently as a part of other Questions such as [this Question](http://stackoverflow.com/q/39706433/642706). – Basil Bourque Sep 26 '16 at 15:39
  • 1
    Is this intended to be a canonical question? If so, it's worth noting that so that the rest of us don't nuke it from orbit. – chrylis -cautiouslyoptimistic- Sep 26 '16 at 15:39
  • @chrylis Yes, so intended. How do you suggest I note it as such? – Basil Bourque Sep 26 '16 at 15:41
  • How about this question @Basil http://stackoverflow.com/questions/43802/how-to-convert-a-date-string-to-a-date-or-calendar-object? Your answer there covers this case also. – Tunaki Sep 26 '16 at 15:41
  • @MarcB and Tunaki: Both are referring to the old date packages, not Java 8. – chrylis -cautiouslyoptimistic- Sep 26 '16 at 15:42
  • @chrylis Top answer yes, but not Basil's one http://stackoverflow.com/a/33405474/1743880 – Tunaki Sep 26 '16 at 15:42
  • @Tunaki Basil's answer doesn't actually answer that question as posed, which is asking about `Date` and `Calendar`. – chrylis -cautiouslyoptimistic- Sep 26 '16 at 15:43
  • 1
    Fair enough, but I think it'd may be better to edit [that canonical](http://stackoverflow.com/a/4216767/1743880) with this special case. Because there are `LocalDateTime.parse` and `ZonedDateTime.parse` without formatter as well. (Ah, I found [that one](http://stackoverflow.com/questions/28352493/convert-iso-8601-date-to-a-standard-string-format) as well). – Tunaki Sep 26 '16 at 15:46
  • 1
    @Tunaki The posts you linked are so broad, so long, address so many issues including some historic issues such as Joda-Time and the legacy date-time classes, that is overkill for such a simple and *common* question of “How do I parse YYYY-MM-DD dates in modern Java?”. And so I wrote this Question and Answer. – Basil Bourque Sep 26 '16 at 15:55
  • The [supposed duplicate](http://stackoverflow.com/q/4216745/642706) is *not* a duplicate. That Question asks about an input string in the format “January 2, 2010” with English words. This Question here is about standard ISO 8601 format, all digits and hyphens, no English words. Furthermore, the Answers for that other Question go on to discuss the complexities of formatting pattern codes which are not at all required in this common case of ISO 8601 strings being discussed here. – Basil Bourque Sep 26 '16 at 18:38

1 Answers1

2

LocalDate.parse

The java.time classes can parse ISO 8601 strings directly, with no need to specify a formatting pattern.

For a date-only value, without a time of day or time zone, use the LocalDate class.

LocalDate ld = LocalDate.parse("2016-03-21");
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • What if you can't guarantee whether the parsed value will have the time/timezone part or not? There doesn't seem to be a parse() method that will correctly parse both "YYYY-MM-DD" and "YYYY-MM-DDTHH:MM+ZZ:VV" for instance. Both of them are valid ISO8601 dates. – Bitcoin Cash - ADA enthusiast Aug 02 '19 at 02:35
  • 1
    @Tiago No, there is not a single way to parse two such *very* different inputs, where one represents a date and the other represents a moment. You could easily check the length of the input to know which is which and choose an appropriate parsing approach. – Basil Bourque Aug 02 '19 at 07:05