无法解析的日期格式

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

Unparsable date format

问题

我正试图解析一个日期字符串,但出现了这个错误:

>java.text.ParseException: 无法解析的日期:"Oct 1, 1997, 12:00:00 AM"

这是我用来解析Date的方法:

public static Date parse(@NonNull String dateString, @NonNull String dateFormat) {
    val sdf = new SimpleDateFormat(dateFormat);
    sdf.setLenient(false);
    try {
        return sdf.parse(dateString);
    } catch (ParseException e) {
        return null;
    }
}

其中dateStringOct 1, 1997, 12:00:00 AM,而 dateFormat 则为 MMM d, yyyy, HH:mm:ss a

为什么它无法成功解析日期?

英文:

I am attempting to parse a date String but get this error:

>java.text.ParseException: Unparseable date: "Oct 1, 1997, 12:00:00 AM"

This is the method I am using to parse the Date:

public static Date parse(@NonNull String dateString, @NonNull String dateFormat) {
    val sdf = new SimpleDateFormat(dateFormat);
    sdf.setLenient(false);
    try {
        return sdf.parse(dateString);
    } catch (ParseException e) {
        return null;
    }
}

Where the dateString is Oct 1, 1997, 12:00:00 AM and the dateFormat is MMM d, yyyy, HH:mm:ss a.

Why is it failing to parse the date?

答案1

得分: 3

我猜你应该使用小写的 "h" 而不是大写的 "H"。小写的 "h" 表示 0-12,而大写的 "H" 表示 0-24。
总体来说,应该是
MMM d, yyyy, hh:mm:ss a

英文:

I guess you should use "h" instead of "H". Lowercase h refers to 0-12 capital one refers to 0-24.
Overall it should be
MMM d, yyyy, hh:mm:ss a

答案2

得分: 3

如果您将SimpleDateFormat更改为DateTimeFormatter,则异常显示错误:

原因:java.time.DateTimeException:发现冲突:字段AmPmOfDay 1与从12:00派生的AmPmOfDay 0不同

对于12:00时间,它期望它是下午。如果您指的是午夜,那么应该是00:00 AM。

英文:

If you change the SimpleDateFormat to DateTimeFormatter the exception shows the error:

Caused by: java.time.DateTimeException: Conflict found: Field AmPmOfDay 1 differs from AmPmOfDay 0 derived from 12:00

For the 12:00 time it expects it to be PM. if you mean midnight instead, it should be 00:00 AM.

答案3

得分: 2

你的代码抛出异常是因为字符串 "Date" 在字符串模式中无效,可以在文档这里查看。

具体而言,如果小时在 0 到 23 之间,则字符串为 HH,但如果使用 1 到 12 的 AM、PM 格式,则应使用 hh

以下是一些参考代码:

class Ideone {
    public static void main(String[] args) throws java.lang.Exception {
        Date x = parse("Oct 1, 1997, 12:00:00 AM", "MMM d, yyyy, hh:mm:ss a");
        System.out.println("X String: " + x); 
    }

    public static Date parse(String dateString, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        sdf.setLenient(false);
        try {
            return sdf.parse(dateString);
        } catch (Exception e) {
            System.out.println("E???");
            return null;
        }
    }
}

如果需要进行编辑,这是 ideone 代码链接:

https://ideone.com/ccwo2Y

英文:

your code is throwing an exception because the string Date is not valid for the string pattern, look in the doc here

concretely, if the hour is in a format between 0-23 then the string is HH, but if you use a 1-12 AM, PM then you have to use hh

here is some ref code:

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Date x = parse("Oct 1, 1997, 12:00:00 AM", "MMM d, yyyy, hh:mm:ss a");
	    System.out.println("X String: " + x); 
    }
    
    public static Date parse(String dateString, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        sdf.setLenient(false);
        try {
            return sdf.parse(dateString);
        } catch (Exception e) {
		    System.out.println("E???");
            return null;
        }
    }
}

and if you need to edit, here the ideone code:

https://ideone.com/ccwo2Y

huangapple
  • 本文由 发表于 2020年7月27日 19:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114821.html
匿名

发表评论

匿名网友

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

确定