【发布时间】:2019-07-16 08:19:26
【问题描述】:
我想将字符串转换为特定格式的 LocalDate (yyyy-MM-dd),我的字符串已经是 ISO_LOCAL_DATE 格式。
代码
private LocalDate myDate;
public MyObject(String name, LocalDate myDate) {...}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
MyObject myObject = new myObject("my object name", LocalDate.parse("2019-03-01", formatter));
错误
java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:NanoOfDay
已经尝试过
object = new Object("object name", (LocalDate) formatter.parse("2019-03-01"));
use SimpleDateFormatter
【问题讨论】:
-
您确定发布了测试代码吗?格式化程序似乎没问题。
-
错误很可能发生在其他地方,在您没有向我们展示的代码中,因为您发布的将
2019-03-01等字符串解析为LocalDate的代码是正确的并且不会抛出一个UnsupportedTemporalTypeException。 -
我打赌你使用的是 joda.time 包而不是 java.time
-
@Lino 刚刚用 java.time 编译了代码,没有任何错误。但它确实接受另一个包含,joda 的 DateTimeFormatter。
-
我只是一个愚蠢的人对不起......我会删除这个帖子。确实,代码运行良好,我找错地方了……抱歉。