[英]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?如何在现代 Java 中解析 YYYY-MM-DD 日期?
I have a Java String of a standard ISO 8601 date in the YYYY-MM-DD format, such as 2016-03-21
.我有一个 YYYY-MM-DD 格式的标准ISO 8601日期的 Java 字符串,例如2016-03-21
。
How can I parse this into the modern Date-Time types in Java, the java.time classes?如何将其解析为 Java 中的现代日期时间类型,即java.time类?
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. java.time
类可以直接解析ISO 8601字符串,无需指定格式模式。
For a date-only value, without a time of day or time zone, use the LocalDate
class.对于没有时间或时区的仅限日期的值,请使用LocalDate
类。
LocalDate ld = LocalDate.parse("2016-03-21");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.