使用 SimpleDateFormat 无法解析的日期

Unparseable date with SimpleDateFormatter

我有一个日期一直给我一个错误

java.text.ParseException: Unparseable date: " 5 April 2017  "

所有其他日期(没有月份)都可以正常工作

我使用的代码如下:

VisitDate=VisitDate.trim();
if (VisitDate.matches(".*[a-z].*")){
    SimpleDateFormat changeDate = new SimpleDateFormat("dd_MMM_yy", Locale.UK);
    //Convert the string to a date
    Date date = changeDate.parse(VisitDate);
    //Reformat the date the way I like it
    SimpleDateFormat dt1 = new SimpleDateFormat("dd_MM_yy");

    //Convert back into a string
    try {
        VisitDate=dt1.format(date);
        if(VisitDate==null){
            SimpleDateFormat dt2 = new SimpleDateFormat("dd MM yy");
            //Convert back into a string
            VisitDate=dt2.format(date);
            if(VisitDate==null){
                SimpleDateFormat dt3 = new SimpleDateFormat("dd MMMM yy");
                //Convert back into a string
                VisitDate=dt3.format(date);
                if(VisitDate==null){
                    SimpleDateFormat dt4 = new SimpleDateFormat("d_MMM_yy");
                    //Convert back into a string
                    VisitDate=dt4.format(date);
                    if(VisitDate==null){
                        SimpleDateFormat dt5 = new SimpleDateFormat("d_MMMM_yy");
                        //Convert back into a string
                        VisitDate=dt5.format(date);
                        if(VisitDate==null){
                            SimpleDateFormat dt6 = new SimpleDateFormat("dd_MMM_yy");
                            //Convert back into a string
                            VisitDate=dt6.format(date);
                            if(VisitDate==null){
                                SimpleDateFormat dt7 = new SimpleDateFormat("dd_MMM_yyyy");
                                //Convert back into a string
                                VisitDate=dt7.format(date);
                                if(VisitDate==null){
                                    SimpleDateFormat dt8 = new SimpleDateFormat("dd_MMMM_yyyy");
                                    //Convert back into a string
                                    VisitDate=dt8.format(date);
                                    if(VisitDate==null){
                                        VisitDate=VisitDate.replaceAll("\s", "");

                                        SimpleDateFormat dt9 = new SimpleDateFormat("dd MMMM yyyy");
                                        //Convert back into a string
                                        VisitDate=dt9.format(date);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        Logger.error(e+"->No visit Date frmo here with the original date as: "+date);
    }
}

您有要像这样解析的输入字符串:" 5 April 2017 "。但是你没有这个输入的模式匹配

首先请trim输入字符串

space

其次,请更改字符串的格式或添加另一个模式来解析此字符串,因为您的月份 April 有 5 个字母,但您没有 MMMMM 的模式。通常月份会缩短为 3 个字母,例如:April -> Apr, March -> Mar ...

您只有一个 SimpleDateFormatter 用于解析:

SimpleDateFormat changeDate = new SimpleDateFormat("dd_MMM_yy", Locale.UK);
//Convert the string to a date
Date date = changeDate.parse(VisitDate);

它使用的格式是 dd_MMM_yy,但是您传递的是 4 位数字的年份。

其余的 SimpleDateFormatters 用于格式化为字符串,而不是从字符串进行解析。只有第一个将被调用,因为它能够在给定有效日期的情况下生成字符串,并且以下空检查将停止使用任何其他格式化程序。

很高兴看到您表达了对现代日期和时间的兴趣类,这里只是让您入门的一个片段:

    String visitDate = " 5 April 2017  ";
    DateTimeFormatter parseFormatter 
            = DateTimeFormatter.ofPattern("d MMMM uuuu", Locale.UK);
    visitDate = visitDate.trim();
    LocalDate date = LocalDate.parse(visitDate, parseFormatter);
    // reformat to the string we like
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd_MM_uu");
    visitDate = date.format(formatter);

结果是

05_04_17

我用小 v 拼写了 visitDate,因为 Java 编码约定建议变量名以小写字母开头。

uu 很微妙,您可能可以忽略它。这是一个有符号的年份,其中 0 等于 1 BC,-1 等于 2 BC 等等。假设 none 的日期那么旧,您可以互换使用 uy

我相信 SimpleDateFormat.format()LocalDate.format() 都不会 return null,所以你所有的 null 检查都是多余的。

Link 进一步阅读:Oracle tutorial: Trail: Date Time