我试图从两个日历的差异中获得时间,并知道结果是否超过一天(86400secs)。问题是有时我会得到很大的负值。
private long calcValidityDuration(XMLGregorianCalendar greg, XMLGregorianCalendar gregStart) {
int minEnd = greg.getMinute();
int minStart = gregStart.getMinute();
int hourStart = gregStart.getHour();
int hourEnd = greg.getHour();
int dayEnd = greg.getDay();
int dayStart = gregStart.getDay();
int monthStart = gregStart.getMonth();
int monthEnd = greg.getMonth();
int yearStart = gregStart.getYear();
int yearEnd = greg.getYear();
Calendar cal = new GregorianCalendar();
cal.set(yearEnd, monthEnd - 1, dayEnd, hourEnd, minEnd);
Calendar calStart = new GregorianCalendar();
calStart.set(yearStart, monthStart - 1, dayStart, hourStart, minStart);
long toRet = (((cal.getTimeInMillis()) - (calStart.getTimeInMillis())) / 1000);
if(toRet >= 86400){
return 86400;
}else{
return toRet;
}
}
这里有一些有问题的日期:
开课日期:30/09/16 07 截止日期:29/09/16 16
开课日期:29/09/16 16 截止日期:29/09/16 16
开课日期:08/09/16 08 截止日期:29/09/16 16
开课日期:29/09/16 16 截止日期:21/11/14 19
开课日期:29/09/16 16 截止日期:2014年3月20日13
答案 0 :(得分:1)
public static long getDistanceDay(Calendar one,Calendar two) {
long time1 = one.getTimeMillis();
long time2 = two.getTImeMillis();
long time;
if(time1 > time2) {
time = time-time2;
}
else {
time = time2 - time1;
}
return time/(1000*60*60*24)
}