将字符串解析为本地日期不使用所需的世纪 [英] Parsing string to local date doesn't use desired century

查看:24
本文介绍了将字符串解析为本地日期不使用所需的世纪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个 DateTimeFormatter:

DateTimeFormatter.ofPattern("ddMMYY")

我想解析字符串 150790 并且出现此错误:

无法从 TemporalAccessor 获取 LocalDate:{DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},java.time.format.Parsed 类型的 ISO

显然,我想获得以下 TemporalAccessor:

{DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990}

你知道为什么我得到的是 2090 年而不是 1990 年吗?

感谢您的帮助

解决方案

由于这个问题实际上是关于新的 java.time-package 而不是 SimpleDateFormat 我将引用以下内容相关部分:

<块引用>

Year:字母的数量决定了下面的最小字段宽度使用哪种填充.如果字母数是两个,则减少使用两位数形式.对于打印,这会输出最右边的两个数字.对于解析,这将使用基值 2000 进行解析,结果是 2000 年到 2099 年(含)范围内的一年.

我们看到 Java-8 使用范围 2000-2099 的默认值,而不是像 SimpleDateFormat 范围 -80 年到 +20 年(相对于今天).>

如果要配置它,则必须使用 appendValueReduced().这是以一种不方便的方式设计的,但可能,请参阅此处:

String s = "150790";//基本范围为 2000-2099 的旧代码日期时间格式器 dtf1 =new DateTimeFormatterBuilder().appendPattern("ddMMyy").toFormatter();System.out.println(dtf1.parse(s));//2090-07-15//基本范围为 1935-2034 的改进代码日期时间格式器 dtf2 =new DateTimeFormatterBuilder().appendPattern("ddMM").appendValueReduced(ChronoField.YEAR, 2, 2, Year.now().getValue() - 80).toFormatter();System.out.println(dtf2.parse(s));//1990-07-15

顺便说一下,如果您真的想要基于周的年份,那么您必须使用 Y 而不是 y 或适当的字段 IsoFields.WEEK_BASED_YEAR.关于您没有任何其他与周相关的字段这一事实,我假设是正常的日历年,而不是基于周的.

I am using this DateTimeFormatter:

DateTimeFormatter.ofPattern("ddMMYY")

I want to parse the string 150790 and I got this error:

Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed

Obviously, I want to get the following TemporalAccessor:

{DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990}

Do you know why I got the year 2090 instead of 1990?

Thanks for your help

解决方案

Since this question is really about new java.time-package and NOT SimpleDateFormat I will cite following relevant section:

Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.

We see that Java-8 uses the range 2000-2099 per default, not like SimpleDateFormat the range -80 years until +20 years relative to today.

If you want to configure it then you have to use appendValueReduced(). This is designed in an inconvenient way, but possible, see here:

String s = "150790";

// old code with base range 2000-2099
DateTimeFormatter dtf1 = 
  new DateTimeFormatterBuilder().appendPattern("ddMMyy").toFormatter();
System.out.println(dtf1.parse(s)); // 2090-07-15

// improved code with base range 1935-2034
DateTimeFormatter dtf2 = 
  new DateTimeFormatterBuilder().appendPattern("ddMM")
  .appendValueReduced(
    ChronoField.YEAR, 2, 2, Year.now().getValue() - 80
  ).toFormatter();
System.out.println(dtf2.parse(s)); // 1990-07-15

By the way, if you really want week-based years then you have to use Y instead of y or the appropriate field IsoFields.WEEK_BASED_YEAR. Regarding the fact that you don't have any other week-related fields I would assume the normal calendar year, not the week-based one.

这篇关于将字符串解析为本地日期不使用所需的世纪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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