Java DateTimeFormatter按预期未解析

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

Java DateTimeFormatter is not parsing as expected

问题

我尝试了 DateTimeFormatter 来解析输入的日期为 dd/MM/yyyy 格式。我已经使用了以下代码:

java.time.format.DateTimeFormatter 无法成功解析日期

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy").withResolverStyle(ResolverStyle.STRICT);

try {
    LocalDate.parse(dateField, dateFormatter);
    return true;
} catch (Exception e) {
    e.printStackTrace();
    return false;
}

return true;

输入:30/04/2018

错误:Caused by: java.time.DateTimeException: 无法从 TemporalAccessor 获取 LocalDate:{MonthOfYear=4,YearOfEra=2018,DayOfMonth=30},类型为 java.time.format.Parsed

这也在闰年中失败。

英文:

I tried DateTimeFormatter to parse input date to dd/MM/yyyy. I have used below code

java.time.format.DateTimeFormatter is failing to parse the date

  DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy").withResolverStyle(ResolverStyle.STRICT);
    
       
            try {
                LocalDate.parse(dateField, dateFormatter);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        
        return true;
    }

Input: 30/04/2018

Error:Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=4, YearOfEra=2018, DayOfMonth=30},ISO of type java.time.format.Parsed

It is also failing for leap years.

答案1

得分: 3

问题是使用.withResolverStyle(ResolverStyle.STRICT)需要使用年份模式uuuu,而不是yyyy(即使用"年份"代替"纪元年份")。

英文:

The issue is using .withResolverStyle(ResolverStyle.STRICT) requires to use year pattern uuuu instead of yyyy (i.e. "year" instead of "year-of-era")

答案2

得分: 1

你基本上有两个选项(在这里,一个是使用你在代码示例中展示的ResolverStyle):

  • 显式使用ResolverStyle.STRICT ⇒ 仅解析年份 u
  • 使用默认的ResolverStyle ⇒ 将解析纪元年 y年份 u

以下示例显示了代码中的差异:

public static void main(String[] args) {
	String date = "30/04/2018";
	// 第一个格式化器,使用纪元年但没有解析样式
	DateTimeFormatter dtfY = DateTimeFormatter.ofPattern("dd/MM/yyyy");
	// 第二个格式化器,使用年份和严格的解析样式
	DateTimeFormatter dtfU = DateTimeFormatter.ofPattern("dd/MM/uuuu")
												.withResolverStyle(ResolverStyle.STRICT);
	// 解析
	LocalDate localDateU = LocalDate.parse(date, dtfU);
	LocalDate localDateY = LocalDate.parse(date, dtfY);
	// 打印结果
	System.out.println(localDateU);
	System.out.println(localDateY);
}

输出为

2018-04-30
2018-04-30

因此,两个DateTimeFormatter都解析了完全相同的String,但没有显式附加ResolverStyle的那个将默认使用ResolverStyle.SMART,根据DateTimeFormatter的JavaDocs

当然,具有年份u)的模式也将被ResolverStyle.SMART解析,因此

DateTimeFormatter.ofPattern("dd/MM/uuuu");

也是一个选择。

关于年代年份之间的区别,可以在这篇帖子中找到很好的解释。

英文:

You basically have two options (here, where one is using the ResolverStyle you are showing in your code example):

  • use a ResolverStyle.STRICT explicitly ⇒ parses year u only
  • use a default ResolverStyle ⇒ year-of-era y or year u will be parsed

The following example shows the differences in code:

public static void main(String[] args) {
	String date = "30/04/2018";
	// first formatter with year-of-era but no resolver style
	DateTimeFormatter dtfY = DateTimeFormatter.ofPattern("dd/MM/yyyy");
	// second one with year and a strict resolver style
	DateTimeFormatter dtfU = DateTimeFormatter.ofPattern("dd/MM/uuuu")
												.withResolverStyle(ResolverStyle.STRICT);
	// parse
	LocalDate localDateU = LocalDate.parse(date, dtfU);
	LocalDate localDateY = LocalDate.parse(date, dtfY);
	// print results
	System.out.println(localDateU);
	System.out.println(localDateY);
}

The output is

2018-04-30
2018-04-30

so both DateTimeFormatters parse the very same String, but the one without a ResolverStyle explicitly attached will use ResolverStyle.SMART by default according to the JavaDocs of DateTimeFormatter.

Of course, a pattern with year (u) will be parsed by ResolverStyle.SMART, too, so

DateTimeFormatter.ofPattern("dd/MM/uuuu");

would be an option as well.

A good explanation of the difference between year-of-era and year can be found in this post.

huangapple
  • 本文由 发表于 2020年9月2日 21:33:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63706705.html
匿名

发表评论

匿名网友

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

确定