2017-06-16 70 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

相关问题