2014-01-17 38 views
9

我执行下面的代码,给了我运行时异常,同时试图使用乔达DateTimeFormatter

"Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot parse "12/17/2017 23:10": Value 23 for clockhourOfHalfday must be in the range [1,12]"

DateTimeFormatter dt = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm"); 
System.out.println(dt.parseDateTime("12/17/2017 23:10")); 

如果我将解析"12/17/2017 02:10"解析日期时间然后成功执行。

所以基本上,我需要解析具有24小时时钟格式的时间。

在此先感谢。

回答

23

h是JodaTime中的半天,正如你可以在例外中看到的那样。
您需要使用H(或可能k):

DateTimeFormatter dt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm"); 
System.out.println(dt.parseDateTime("12/17/2017 23:10")); 

还有就是DateTimeFormat API的更多信息。

+0

它正在工作。谢谢:) – Rahul

+3

(这也是SimpleDateFormat中的12小时时钟...) –

2

它也可以用简单的日期格式来实现。

DateFormat readDate = new SimpleDateFormat("MM/dd/yyyy HH:mm"); 
Date date = readDate.parse("12/17/2017 23:10"); 
DateFormat writeDate = new SimpleDateFormat("MM/dd/yyyy HH:mm"); 
System.out.println(writeDate.format(date));