Joda 时间 - 解析字符串抛出 java.lang.IllegalArgumentException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15642053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Joda time - Parsing string throws java.lang.IllegalArgumentException
提问by Sérgio Michels
Shouldn't a String
formatted with a specific DateTimeFormatter
be able to be parsed using LocalDateTime.parse()
?
不应该String
使用特定格式DateTimeFormatter
来解析LocalDateTime.parse()
吗?
Test
测试
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis()
LocalDateTime ldt = new LocalDateTime()
String val = ldt.toString(formatter)
System.out.println(val) //2013-03-26T13:10:46
// parse() throws java.lang.IllegalArgumentException: Invalid format: "2013-03-26T13:10:46" is too short
LocalDateTime nldt = LocalDateTime.parse(val, formatter)
采纳答案by Magnilex
Have a look at the JavaDoc:
看看JavaDoc:
Returns a basic formatter that combines a basic date and time without millis, separated by a 'T' (yyyyMMdd'T'HHmmssZ). The time zone offset is 'Z' for zero, and of the form '±HHmm' for non-zero. The parser is strict by default, thus time string 24:00 cannot be parsed.
返回一个基本的格式化程序,它结合了一个没有毫秒的基本日期和时间,由一个 'T' (yyyyMMdd'T'HHmmssZ) 分隔。时区偏移量为零时为“Z”,非零时为“±HHmm”形式。默认情况下,解析器是严格的,因此无法解析时间字符串 24:00。
The key here seems to be The time zone offset is 'Z' for zero, and of the form '±HHmm' for non-zero. Joda Time obviously expects time zone information for parse()
.
这里的关键似乎是时区偏移是 'Z' 表示零,形式 '±HHmm' 表示非零。Joda Time 显然需要parse()
.
I appended "Z"
to your parsed date String and it works better:
我附加"Z"
到您解析的日期字符串中,效果更好:
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
LocalDateTime ldt = new LocalDateTime();
String val = ldt.toString(formatter);
System.out.println(val);
val += "Z";
LocalDateTime nldt = LocalDateTime.parse(val, formatter);
System.out.println(nldt.toString());
Output is:
输出是:
2013-03-26T17:50:06
2013-03-26T17:50:06.000
回答by Clockwork-Muse
The formatter also throws the error if formatter.parseLocalDateTime(val)
is called, which is what LocalDateTime.parse(...)
is calling directly (ie, the naive call).
如果formatter.parseLocalDateTime(val)
被调用,格式化程序也会抛出错误,这就是LocalDateTime.parse(...)
直接调用(即,naive 调用)。
So, then, it's a factor of the format that is expected - which in this case is yyyy-MM-dd'T'HH:mm:ssZZ
; basically, it's complaining that you haven't passed a timezone.
因此,这是预期格式的一个因素 - 在这种情况下是yyyy-MM-dd'T'HH:mm:ssZZ
; 基本上,它抱怨你没有通过时区。
I don't know if this actually qualifies as a 'bug'; in the short term, obviously a custom format may be used, or look at ISODateTimeFormat.localDateOptionalTimeParser(), which appears to be what you want (it's the default parser used by LocalDateTime
).
我不知道这是否真的可以称为“错误”;在短期内,显然可以使用自定义格式,或者查看ISODateTimeFormat.localDateOptionalTimeParser(),这似乎是您想要的(它是 使用的默认解析器LocalDateTime
)。