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();
}
我已经尝试了上面的代码来寻找两个时间值之间的差异,但它不工作,这个错误即将到来:
12-25 09:00:56.537: W/System.err(1642): java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
=>检查您的导入,正确的是java.util.Date
你可以试着在
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));
我建议您使用优秀的Joda Time库,而不是有缺陷的java.util.Date和friends。你可以简单地写
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();