乔达时间&基于枢轴来正确解析两位数年份 [英] Joda Time & parsing two digit year correctly based on pivot
问题描述
我有以下(稍微缩略)的代码来解析 String
输入到Joda时间 DateTime
对象。我需要妥善处理多种格式,包括4&两位数的年份。
I have the following (somewhat abbreviated) code to parse String
input into a Joda Time DateTime
object. I need to properly handle multiple formats, including four & two digit years.
setupValidDateFormats();
DateTime date = convertToDateTime(thisField.getText());
if (date != null) {
thisField.setText(date.toString("MM/dd/yyyy"));
} else {
System.out.println("Invalid date");
}
private void setupValidDateFormats() {
DateTimeParser[] formats = {
DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
DateTimeFormat.forPattern("MM/dd/yy").getParser(),
DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
DateTimeFormat.forPattern("MM-dd-yy").getParser(),
DateTimeFormat.forPattern("MMddyyyy").getParser(),
DateTimeFormat.forPattern("MMddyy").getParser()
};
formatter = new DateTimeFormatterBuilder().append(null, formats).appendTwoDigitYear(1950, true).toFormatter()
.withLocale(Locale.US);
}
private DateTime convertToDateTime(String input) {
if (isNullOrEmpty(input)) {
return null;
}
DateTime date;
try {
// When you parse a date, it'll throw an exception for not only invalid formats,
// but for invalid values such as 09/31/2013 or 02/30/2013. Leap year is included as well.
date = formatter.parseDateTime(input);
} catch (Exception e) {
// User input a date in the incorrect format, or an invalid value.
// Example: 02/30/2013 is invalid.
date = null;
}
return date;
}
我遇到的问题是当用户输入 120100
,我希望它能够正确解析,最终在12/01/1900输出。但是, formatter.parseDateTime(input);
in convertToDateTime
抛出一个 IllegalArgumentException
,而该方法返回 null
。
The issue I'm having is that when the user enters 120100
, I expect it to be able to parse correctly and ultimately output at 12/01/1900. However, formatter.parseDateTime(input);
in convertToDateTime
throws an IllegalArgumentException
instead, and the method returns null
.
可能值得注意的是,如果我删除 appendTwoDigitYear
从 DateTimeFormatterBuilder
链,输入成功解析,但 120100
成为$ code> 12/01/0000 ,但这不是我需要的。
Probably worth noting that if I remove the appendTwoDigitYear
from the DateTimeFormatterBuilder
chain, the input does parse successfully, but 120100
becomes 12/01/0000
, but that's not what I need.
我误解了Joda Time API吗?不应 appendTwoDigitYear
,1950年的枢轴处理00年数字为1900年。
Am I misunderstanding the Joda Time API? Shouldn't appendTwoDigitYear
with the 1950 pivot handle the 00 two year digit as 1900?
推荐答案
p>当您使用
DateTimeFormat.forPattern("MMddyyyy")
它可以读取像120100
之类的日期。它的作用是阅读
MM
, 01
for dd
和 00
作为 yyyy
的文字值,即。 0
从年份开始 0
,所以 0000
。
it can read a date like "120100"
. What it does is read the 12
for MM
, the 01
for dd
, and 00
as a literal value for yyyy
, ie. 0
starting at year 0
, so 0000
.
因为您的解析器中的
数组,它将被用来解析你的日期字符串。 MMddyy
之前出现该模式
Because that pattern appears before the MMddyy
in your parsers
array, it will be used to parse your date string.
你想要做什么,连同重新排序或删除这个格式,都是使用一个枢纽的年份。
What you'll want to do, along with reordering or removing that format, is use a pivot year.
public static void main(String[] args) throws Exception {
String str = "120100";
DateTimeParser[] formats = {
DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
DateTimeFormat.forPattern("MM/dd/yy").getParser(),
DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
DateTimeFormat.forPattern("MM-dd-yy").getParser(),
DateTimeFormat.forPattern("MMddyy").getParser()
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, formats).toFormatter()
.withPivotYear(1950).withLocale(Locale.US);
DateTime dateTime = formatter.parseDateTime(str);
System.out.println(dateTime);
}
打印
1900-12-01T00:00:00.000-05:00
我还在试图找出 appendTwoDigitYear
的作用。
I'm still trying to figure out how appendTwoDigitYear
works.
这篇关于乔达时间&基于枢轴来正确解析两位数年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!