如何将“Nov 20, 2016 12:00:00 AM”解析为LocalDateTime?

huangapple go评论69阅读模式
英文:

How do I parse "Nov 20, 2016 12:00:00 AM" to LocalDateTime?

问题

I found a very interesting bug or something like that.

I use this date format MMM d, yyyy hh:mm:ss a, and it will print the date like this:

Aug 13, 2020 01:19:50 pm

But when I parse Nov 20, 2016 12:00:00 AM to LocalDateTime, it throws the exception:

java.time.format.DateTimeParseException: Text 'Nov 20, 2016 12:00:00 AM' could not be parsed at index 22.

After I change 'AM' to 'am', it works perfectly! So, LocalDateTime literally cannot parse the date because of uppercase letters?
And how can I solve this problem without replacing 'AM' to 'am' and 'PM' to 'pm'?

EDIT

SimpleDateTime format doesn't have this problem; it ignores the uppercase or lowercase letters. I mean uppercase or lowercase. And I would not like to convert Date to LocalDateTime.

EDIT 2

MMM d, yyyy hh:mm:ss A replacing 'a' with 'A' also didn't work.

英文:

I found very interesting bug or something like that.

I use this date format MMM d, yyyy hh:mm:ss a and it will prints date like this

Aug 13, 2020 01:19:50 pm

But, when I parse Nov 20, 2016 12:00:00 AM to LocalDateTime, it throws the exception

java.time.format.DateTimeParseException: Text 'Nov 20, 2016 12:00:00 AM' could not be parsed at index 22.

After I change 'AM' to 'am' it works perfectly!
so LocalDateTime literally cannot parse date because of uppercase a̶p̶e̶s̶ letters?
And how can I solve this problem, without replacing 'AM' to 'am' and 'PM' to 'pm'

EDIT

SimpleDateTime format doesn't have this problem, he ignores the a̶p̶e̶s̶ letters register (i mean uppercase or lowercase) And I would not like to convert Date to LocalDateTime

EDIT 2

MMM d, yyyy hh:mm:ss A replace 'a' to 'A' also didn't work

答案1

得分: 7

需要以不区分大小写的方式解析它。此外,请确保使用英语区域设置(例如 Locale.ENGLISHLocale.US 等),因为元素的名称在相应的区域设置中使用本地化字符串表示。

示例:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // 给定日期时间字符串
        String strDateTime = "Nov 20, 2016 12:00:00 AM";

        // 定义格式化器
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                        .parseCaseInsensitive()
                                        .appendPattern("MMM d, u h:m:s a")
                                        .toFormatter(Locale.ENGLISH);

        // 使用格式化器将给定日期时间解析为 LocalDateTime
        LocalDateTime ldt = LocalDateTime.parse(strDateTime, formatter);

        System.out.println(ldt);
    }
}

输出:

2016-11-20T00:00
英文:

You need to parse it in a case-insensitive way. Also, make sure to use an English locale (e.g. Locale.ENGLISH, Locale.US etc.) because the names of elements are represented with localized strings in the corresponding locales.

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Given date-time string
        String strDateTime = "Nov 20, 2016 12:00:00 AM";

        // Define the formatter
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        		 						.parseCaseInsensitive()
                                        .appendPattern("MMM d, u h:m:s a")                                       
                                        .toFormatter(Locale.ENGLISH);                                     

        // Parse the given date-time into LocalDateTime using formatter
        LocalDateTime ldt = LocalDateTime.parse(strDateTime, formatter);

        System.out.println(ldt);
    }
}

Output:

2016-11-20T00:00

huangapple
  • 本文由 发表于 2020年8月13日 15:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63390285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定