将字符串转换为 LocalDate --> 不支持的字段:NanoOfDay:

Transform String to LocalDate --> Unsupported field: NanoOfDay :

我想将一个字符串转换为特定格式的 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: Unsupported field: NanoOfDay

已经尝试过

object = new Object("object name", (LocalDate) formatter.parse("2019-03-01"));

use SimpleDateFormatter

If the String is formatted like ISO_LOCAL_DATE, you can parse the String directly, no need conversion.

在Java-8

 public static void main(String[] argv) {
            String date = "2019-03-01";
            LocalDate localDate = LocalDate.parse(date);
            System.out.println(localDate);
        }

输出:

2019-03-01