2014-01-17 37 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));