DateTimeFormatter无法解析德语中没有前导零的日期 [英] DateTimeFormatter Cannot Parse Dates Without Leading Zeroes in German

查看:159
本文介绍了DateTimeFormatter无法解析德语中没有前导零的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的客户今天发现了一个有趣的错误。请考虑以下方法:

Our client found an interesting bug today. Consider the following method:

final DateTimeFormatter englishFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.ENGLISH);
System.out.println(LocalDate.parse("04/01/17", englishFormatter));
System.out.println(LocalDate.parse("4/1/17", englishFormatter));

final DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.GERMAN);
System.out.println(LocalDate.parse("01.04.17", germanFormatter));
System.out.println(LocalDate.parse("1.4.17", germanFormatter));

(如果您不知道,这些确实是英语和德语的正确日期。我会甚至说它们是相同的日期[2017年4月1日]。请花点时间考虑一下你认为这个应用程序应该返回的内容。)

(If you don't know, these are indeed correct dates in English and German. I'd even say they're the same date [April 1 2017]. Take a moment to consider what you'd think this application should return.)

它返回的是以下内容:

Exception in thread "main" java.time.format.DateTimeParseException: Text '1.4.17' could not be parsed at index 0
    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 Main.main(Main.java:20)

英语日期格式使用和不使用前导零。德语格式仅适用于前导零。

The English date format works with and without leading zeroes. The German format works only with leading zeroes.

我似乎找不到一个属性来改变这种行为才能正常工作。

I can't seem to find a property to change this behavior to work correctly.

如何让 DateTimeFormatter 了解没有前导零的德语日期?

How can I make the DateTimeFormatter understand German dates without leading zeroes?

注意:我们的应用程序支持多种语言环境,因此使用特定的 DateTimeFormatter (例如 DateTimeFormatter.ofPattern (dMyy))完全没有问题,特别是因为我们要解析默认日期格式。

Note: Our application supports multiple locales, so using a specific DateTimeFormatter (like DateTimeFormatter.ofPattern("d.M.yy")) is completely out of the question, especially since we want to parse the default date format.

推荐答案

一种解决方案是捕获DateTimeParseException,然后再次使用修改/缩减的日期模式。

One solution is to trap the DateTimeParseException and then try again with a modified/reduced date pattern.

import java.text.Format;
import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.FormatStyle;
import java.util.ListResourceBundle;
import java.util.Locale;

public class Demo {
    public static void main(String[] args) {

        Locale[] locales = {Locale.ENGLISH, Locale.GERMAN};
        for (Locale locale : locales)
        {
            DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale);
            String [] englishDates = {"01/04/17","1/4/17"};
            String [] germanDates = {"01.04.17","1.4.17"};

            String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT,null, IsoChronology.INSTANCE, locale);
            System.out.println("Locale " + locale.getDisplayName() + ": Default Date Pattern (short): " + datePattern);

            String[] dates = null;

            if (locale == Locale.ENGLISH)
                dates = englishDates;
            else
                dates = germanDates;

            for (String date : dates) {
                try {
                    System.out.printf("  " + date + " -> ");
                    System.out.println(LocalDate.parse(date, formatter));
                }
                catch (DateTimeParseException e)
                {
                    System.out.println("Error!");
                    // Try alternate pattern
                    datePattern = datePattern.replace("dd", "d").replace("MM", "M");
                    System.out.println("  Modified Date Pattern (short): " + datePattern);
                    // Allow single digits in day and month
                    DateTimeFormatter modifiedFormatter = DateTimeFormatter.ofPattern(datePattern);
                    System.out.println("  " + date + " -> " + LocalDate.parse(date, modifiedFormatter));  // OK
                }
            }
        }
    }
}

这给出:

Locale English: Default Date Pattern (short): M/d/yy
  01/04/17 -> 2017-01-04
  1/4/17 -> 2017-01-04
Locale German: Default Date Pattern (short): dd.MM.yy
  01.04.17 -> 2017-04-01
  1.4.17 -> Error!
  Modified Date Pattern (short): d.M.yy
  1.4.17 -> 2017-04-01

这篇关于DateTimeFormatter无法解析德语中没有前导零的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆