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

时间秒杀一切 提交于 2019-12-13 22:38:59

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/39706895/parse-date-only-value-in-iso-8601-format-in-java-time-yyyy-mm-dd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!