JAVA计算两个日期之间相差的天数
这是本人第一次写博客,主要计算两个日期之间相差的天数
import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
public class Demo {
public static void main(String[] args) throws ParseException {
Date date1 = DateUtils.parseDate("2013-11-13", "yyyy-MM-dd");
Date date2 = DateUtils.parseDate("2013-12-09", "yyyy-MM-dd");
int day = subDate(date1, date2);
System.out.println(day);
}
/**
* 使用结束日期减去开始日期,得到相差的天数
* @param date
* @return
*/
public static int subDate(Date starttime,Date endtime) {
if(starttime==null || endtime==null){
return 0;
}
long temp = endtime.getTime() - starttime.getTime();
if (temp > 0) {
return (int) (temp / (24 * 60 * 60 * 1000));
} else {
return (int) (temp / (24 * 60 * 60 * 1000)) - 1;
}
}
}
还没有评论,来说两句吧...