2012-12-25 59 views
0
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss"); 

try { 
    Date date = (Date) format.parse(a); 
    Date date1 = (Date) format.parse(b1); 
    Date date2 = (Date) format.parse(c); 

    long diff1 = date1.getTime()-date.getTime(); 
    long diff2 = date2.getTime()- date.getTime(); 

    bundle.putLong("df1", diff1); 
    bundle.putLong("df2", diff2); 

} catch (ParseException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

我已經嘗試了上面的代碼查找兩個時間值,但它不工作的區別,這個錯誤來了:如何在android中查找兩個時間值的區別?

12-25 09:00:56.537: W/System.err(1642): java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date 

回答

0
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date 

=>檢查進口,正確的是java.util.Date

0

你可以嘗試得到像下面

java.sql.Date date = new java.sql.Date(format.parse(a)); 
    java.sql.Date date1 = new java.sql.Date(format.parse(b1)); 
    java.sql.Date date2 = new java.sql.Date(format.parse(c)); 
0

我會建議您使用卓越的Joda Time庫而不是有缺陷的java.util.Date和朋友。你可以簡單地寫

import java.util.Date; 
import org.joda.time.DateTime; 
import org.joda.time.Days; 

Date past = new Date(110, 5, 20); // June 20th, 2010 
Date today = new Date(110, 6, 24); // July 24th 
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); 
相關問題