2017-06-16 66 views
-1

我想比較兩個基於時間的因此我只是將時間轉換爲日期。java.text.ParseException:無法解析的日期:「9:30 AM」(在偏移量5)

我有分析時是這樣的:

SimpleDateFormat h_mm_a = new SimpleDateFormat("h:mm a"); 
Date d1 = h_mm_a.parse(txtTimeFrom.getText().toString()); 
Date d2 = h_mm_a.parse(txtTimeTo.getText().toString()); 
if(d1.compareTo(d2)<0){ 
    .................... 
} 
else{ 
    Toast.makeText(ExecutiveRouteTracking.this,"Invalid Time",Toast.LENGTH_SHORT).show(); 
} 

它會拋出像

java.text.ParseException: Unparseable date: 「9:30 AM」 (at offset 5) 

例外,任何一個可以幫助我解決問題比較兩個時間?

回答

0

與喬達時間嘗試

如果OP Java具有小於或等於7
+0

都不行由於當喬達限制到Java 8? – Akshay

+1

@Akshay

String currentTime = new SimpleDateFormat("HH:mm").format(new Date()); DateTimeFormatter parseFormat = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter(); LocalTime localTime = LocalTime.parse(currentTime, parseFormat); LocalTime limit = new LocalTime("14:00"); return localTime.isAfter(limit); 
Tom

+0

糟糕...它被添加到jdk 8..DateTimeFormatter類 – Akshay

相關問題