Java 8 DateTimeFormatter两位数18年解析为0018而不是2018年? [英] Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?

查看:632
本文介绍了Java 8 DateTimeFormatter两位数18年解析为0018而不是2018年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java 8,下面的代码将18解析为年份0018而不是2018。

With Java 8, the code below parses "18" into year "0018" instead of "2018".

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y");
return LocalDate.parse(date, formatter);

输入日期为01/05/18。

input date is "01/05/18".

1)为什么结果为0018? DateTimeFormatter 不遵循80-20规则吗?

1) why the result is "0018"? Does DateTimeFormatter not follow the 80-20 rule?

2)如何控制SimpleDateFormat解析为19xx或20xx?谈到 SimpleDateFormat。 set2DigitYearStart(Date)可用于修复年份。是否有类似于 DateTimeFormatter 的内容?

2) How to control SimpleDateFormat parse to 19xx or 20xx? talked about SimpleDateFormat.set2DigitYearStart(Date) can be used to fix the year. Is there something similar to that for DateTimeFormatter?

我希望M / d / y将解析两者2年和4位数年。

I was hoping "M/d/y" will parse both 2 and 4 digit years.

M / d / yy抛出4位数年份的异常并将01/05/97解析为2097-01-05 。理想情况下,这应解析为1997-01-05。

"M/d/yy" throws Exception for 4 digit years and parses "01/05/97" to "2097-01-05". Ideally this should be parsed to "1997-01-05".

M / d / yyyy抛出异常2位数。

"M/d/yyyy" throws Exception for 2 digit years.

推荐答案

没有一个字符串 y u 这将允许您解析两位和四位数年份。但是,您可以使用格式模式字符串中的可选部分来指定可能存在两位或四位数年份:

There is not a single string of y or u that will allow you to parse both two and four digit years. However, you may use optional parts in the format pattern string to specify that a two or four digit year may be present:

public static LocalDate parseDateString(CharSequence date) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/[uuuu][uu]");
    return LocalDate.parse(date, formatter);
}

试一试:

    System.out.println(parseDateString("01/05/18"));
    System.out.println(parseDateString("01/06/2018"));

此印刷:

2018-01-05
2018-01-06

In您需要将四位数年份放在首位的格式模式字符串。使用相反的顺序,当尝试解析四位数年份时,格式化程序将解析两位数字,确定它是否成功到目前为止,然后在两位数之后抱怨未解析的文本。

In the format pattern string you need to put the four digit year first. With the opposite order, when trying to parse a four digit year, the formatter will parse two digits, decide it was successful this far, and then complain about unparsed text after the two digits.

如果您想要更精确地控制两位数年份的解释:

If you want more precise control over how two digit years are interpreted:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("M/d/")
            .optionalStart()
            .appendPattern("uuuu")
            .optionalEnd()
            .optionalStart()
            .appendValueReduced(ChronoField.YEAR, 2, 2, 1920)
            .optionalEnd()
            .toFormatter();

在上面的方法中使用这个格式化器让我们试试:

Using this formatter in the above method let’s try:

    System.out.println(parseDateString("01/05/22"));

这打印:

1922-01-05

以1920为基础(在我的示例代码中) )将导致两位数的年份在1920年到2019年的间隔期间结束。根据您的要求调整价值。

Giving 1920 as base (as in my example code) will cause two digit years to end up in the interval from 1920 through 2019. Adjust the value to your requirements.

这篇关于Java 8 DateTimeFormatter两位数18年解析为0018而不是2018年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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