使用pivotYear以多个格式解析日期,设置为2位数年 [英] Parsing date by multiple formats with pivotYear set up for 2 digit years

查看:388
本文介绍了使用pivotYear以多个格式解析日期,设置为2位数年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法实现简单解析日期。要求是允许输入二位数或四位数字。当输入两位数字时,将分割日期用于决定明年1月份的哪个世纪。这是我到目前为止:

  DateTime now = new DateTime(); 
int pivotYear = now.getYear() - 49; // 2013 - 49 = 1964其中49是
DateTimeParser [] parsers = {
DateTimeFormat.forPattern(dd / MM / yy)。withPivotYear(pivotYear).withLocale(new Locale(en ,NZ))getParser(),
DateTimeFormat.forPattern(dd / MM / yyyy)withLocale(new Locale(en,NZ))getParser()
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder()。append(null,parsers).toFormatter();
DateMidnight birthDate = new DateMidnight(formatter.parseDateTime(dateOfBirth));

不幸的是,没有做我期望的。假设今天的日期(11/04/2013)和 dateOfBirth =01/01/14它返回 2014-01-01T00:00 :00.000 + 13:00 。预期的结果将是 1914-01-01T00:00:00.000 + 13:00



当我看附加方法的JavaDoc 我看到这句话


打印机和解析器接口是
格式化API的低级部分。通常,实例是从另一个
格式化程序中提取的。但是请注意,此方法将不会提取任何格式化程序特定信息,例如
作为语言环境,时区,年表,偏移解析或pivot / default
年。


所以我决定将Pivot的东西移动到 DateTimeFormatterBuilder 类中,现在的代码如下所示:

  DateTimeParser [] parsers = {
DateTimeFormat.forPattern(dd / MM / yy)。getParser() ,
DateTimeFormat.forPattern(dd / MM / yyyy)。getParser()
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder()。append(null,parsers).appendTwoDigitYear(pivotYear).toFormatter()。withLocale(new Locale(en,NZ));

不幸的是,这并不排除问题。相反,这次在


java.lang.IllegalArgumentException:格式无效:01/01/14太短
在org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)


从同一个javadoc我得到这个句子


附加打印机和一组匹配的解析器。解析时,选择列表中的第一个解析器进行解析。如果失败,则选择下一个,依此类推。如果这些解析器都不成功,则返回解析器失败的位置。


基于此,第一个解析器应该接收到该作业,但是它看起来像是第二个被触发,并且它失败,因为它期望更长的一年。



任何帮助将深深感激。
Cheers
Tomas

解决方案

所以看起来没有人有足够的兴趣来回答,所以我不得不用自己来说:-)。
我应该在JavaDoc中滚动一下,我应该没有时间的答案。还有另一个 appendTwoDigitYear 方法,将完成我之后的工作。



所以这里是我现在使用的代码



appendTayDeO(1).appendLiteral(/)。appendMonthOfYear(1).appendLiteral(/)。appendTwoDigitYear(pivotYear,true)

.toFormatter()。withLocale(new Locale(en,NZ));
DateMidnight birthDate = new DateMidnight(formatter.parseDateTime(dateOfBirth));

希望将来可以帮助某人。



Cheers
Tomas


I am having trouble to implement "simple" parsing of a date. The requirement is to allow enter year in two or four digits. When two digits are entered then make the split date for deciding into what century it belongs the first of January next year. Here is what I have so far:

DateTime now = new DateTime();
int pivotYear = now.getYear() - 49; // 2013 - 49 = 1964 where 49 is the
DateTimeParser[] parsers = {
    DateTimeFormat.forPattern("dd/MM/yy").withPivotYear(pivotYear).withLocale(new Locale("en", "NZ")).getParser(),
    DateTimeFormat.forPattern("dd/MM/yyyy").withLocale(new Locale("en", "NZ")).getParser() 
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
DateMidnight birthDate = new DateMidnight(formatter.parseDateTime(dateOfBirth));

Unfortunatelly it is not doing what I would expect. Let's say for today's date (11/04/2013) and dateOfBirth = "01/01/14" it returns 2014-01-01T00:00:00.000+13:00. The expected result would be 1914-01-01T00:00:00.000+13:00

When I look at the JavaDoc for the append method I see this sentence

The printer and parser interfaces are the low-level part of the formatting API. Normally, instances are extracted from another formatter. Note however that any formatter specific information, such as the locale, time-zone, chronology, offset parsing or pivot/default year, will not be extracted by this method.

So I have decided to move the Pivot stuff into the DateTimeFormatterBuilder class so now the code looks like this:

DateTimeParser[] parsers = { 
    DateTimeFormat.forPattern("dd/MM/yy").getParser(),
    DateTimeFormat.forPattern("dd/MM/yyyy").getParser()
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).appendTwoDigitYear(pivotYear).toFormatter().withLocale(new Locale("en", "NZ"));

Unfortunatelly this is not sorting the issue. On contrary it is failing this time on

java.lang.IllegalArgumentException: Invalid format: "01/01/14" is too short at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)

from the same javadoc I get this sentence

Appends a printer and a set of matching parsers. When parsing, the first parser in the list is selected for parsing. If it fails, the next is chosen, and so on. If none of these parsers succeeds, then the failed position of the parser that made the greatest progress is returned.

Based on this the first parser should have pick up the job but it looks like the second one is triggered and it fails because it expects longer year.

Any help would be deeply appreciated. Cheers Tomas

解决方案

So it looks like no one is interested enough to answer so I had to figure it by myself :-). I should be scrolling a bit down in the JavaDoc and I should have the answer in no time. There is another appendTwoDigitYear method which will do the job I am after.

So here is the code I am using right now

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendDayOfMonth(1).appendLiteral("/").appendMonthOfYear(1).appendLiteral("/").appendTwoDigitYear(pivotYear, true).toFormatter().withLocale(new Locale("en", "NZ"));
DateMidnight birthDate = new DateMidnight(formatter.parseDateTime(dateOfBirth));

Hope it will help someone in the future.

Cheers Tomas

这篇关于使用pivotYear以多个格式解析日期,设置为2位数年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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