I want to transform a String to a LocalDate in a specific format (yyyy-MM-dd
), My string is already in format of ISO_LOCAL_DATE.
Code
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));
Error
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: NanoOfDay
Already tried
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.
In Java-8
public static void main(String[] argv) {
String date = "2019-03-01";
LocalDate localDate = LocalDate.parse(date);
System.out.println(localDate);
}
Output:
2019-03-01