字符串转LocalDateTime异常

文章目录

    • 现象
    • 解决方案
    • 拓展测试
    • 总结:

现象

当使用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));

拓展测试

  • 格式为"yyyy-MM-dd HH:mm:ss", 字符串也带时分秒"2020-07-07 12:25:11"时,
    LocalDate和LocalDateTime都能格式化到十分秒的字符时间.
    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
  • 格式为"yyyy-MM-dd", 字符串也带时分秒"2020-07-07 12: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)
  • 格式为"yyyy-MM-dd HH:mm:ss", 字符串也带时分秒"2020-07-07"时
    LocalDate格式化报错
    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)
  • Date格式化也正则和字符串十分秒不匹配
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)

总结:

  • LocalDateTime格式化时, 正则和字符串都必须精确到时分秒
  • LocalDate格式化时, 只要正则和字符串格式对应,是否精确到十分秒都可以.
  • Date格式化时, 只要正则和字符串格式对应, 是否精确到十分秒都可以.

你可能感兴趣的:(EX)