当使用LocalDateTime 与字符串互转异常, 使用LocalDateTime 字符串必须带时分秒.
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = "2020-07-07";
System.out.println("LocalDate: " + LocalDate.parse(dateStr, df));
System.out.println("LocalDateTime: " + LocalDateTime.parse(dateStr, df));
}
Connected to the target VM, address: '127.0.0.1:50078', transport: 'socket'
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-07-07' could not be parsed at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at com.example.LocalDateTest.main(LocalDateTest.java:19)
当使用LocalDate时就不会报错
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = "2020-07-07 23:19:46";
System.out.println("LocalDate: " + LocalDate.parse(dateStr, df));
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = "2020-07-07 12:25:11";
System.out.println("LocalDate: " + LocalDate.parse(dateStr, df));
System.out.println("LocalDateTime: " + LocalDateTime.parse(dateStr, df));
}
结果
LocalDate: 2020-07-07
LocalDateTime: 2020-07-07T12:25:11
LocalDate和LocalDateTime格式化都报错
LocalDate
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = "2020-07-07 23:19:46";
System.out.println("LocalDate: " + LocalDate.parse(dateStr, df));
// System.out.println("LocalDateTime: " + LocalDateTime.parse(dateStr, df));
}
结果
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-07-07 23:19:46' could not be parsed, unparsed text found at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at com.example.LocalDateTest.main(LocalDateTest.java:19)
LocalDateTime
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = "2020-07-07 23:19:46";
//System.out.println("LocalDate: " + LocalDate.parse(dateStr, df));
System.out.println("LocalDateTime: " + LocalDateTime.parse(dateStr, df));
}
结果
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-07-07 23:19:46' could not be parsed, unparsed text found at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at com.example.LocalDateTest.main(LocalDateTest.java:21)
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = "2020-07-07";
System.out.println("LocalDate: " + LocalDate.parse(dateStr, df));
//System.out.println("LocalDateTime: " + LocalDateTime.parse(dateStr, df));
}
结果
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-07-07' could not be parsed at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at com.example.LocalDateTest.main(LocalDateTest.java:19)
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = "2020-07-07";
SimpleDateFormat objSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = objSDF.parse(dateStr);
System.out.println("date: " + date);
结果
Exception in thread "main" java.text.ParseException: Unparseable date: "2020-07-07"
at java.text.DateFormat.parse(DateFormat.java:366)
at com.example.LocalDateTest.main(LocalDateTest.java:27)