简体   繁体   中英

Parse date-only value in ISO 8601 format in java.time (YYYY-MM-DD)

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM