Parsing date by multiple formats with pivotYear set up for 2 digit years

 ̄綄美尐妖づ 提交于 2019-12-11 02:16:39

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/15939939/parsing-date-by-multiple-formats-with-pivotyear-set-up-for-2-digit-years

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!