JAVA中经常会计算两个天数之间相差几天,下面代码中实现了此功能
在这里插入代码片
public static int daysBetween(String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000360024);
return Integer.parseInt(String.valueOf(between_days));
}
如果要计算几小时或者几分钟,只需要对最后(time2-time1)/(1000360024);此处进行修改。