2015-11-05 97 views
1

这个代码是抛出 “无效的格式” 或 “畸形的” 异常:JodaTime抛出:IllegalArgumentException:无效的格式

代码:

strDate = "21/10/2015 12:00:00 AM"; 
format = "dd/MM/yyyy hh:mm:ss a"; 

DateTime.parse(strDate, DateTimeFormat.forPattern(format)).toDate(); 

例外:

java.lang.IllegalArgumentException: Invalid format: "21/10/2015 12:00:00 AM" 
is malformed at "AM" 

有什么不对?

我尤斯乔达时间:2.8.1

(我已经搜查,我认为这是为strDate正确的模式)

回答

4

AM/PM标记的文字可能是不同的的默认语言环境。你可以做

Date date = 
    DateTime.parse(strDate, DateTimeFormat.forPattern(format).withLocale(Locale.US)).toDate(); 
+0

好吧我试试这个代码,我评论。 –

+0

确定此代码工作,谢谢,additionaly我更新了JodaTime版本到2.9 –

3

取决于语言环境,你可以指定区域时区

这些应该为你工作:

DateTime.parse(strDate, DateTimeFormat.forPattern(format).withLocale(Locale.US)).withZone(DateTimeZone.UTC).toDate(); 

DateTime.parse(strDate, DateTimeFormat.forPattern(format).withLocale(Locale.US)).toDate(); 
+0

是的,你说得对,谢谢。 –

+0

不客气。 – jfun

相关问题