java计算两个日期间的天数
1、精确到年月日
/**
* 获取两个日期间的天数,精确到年月日
* @param start
* @param end
* @return
*/
public static int daysBetween(Date start, Date end) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
start = sdf.parse(sdf.format(start));
end = sdf.parse(sdf.format(end));
} catch (ParseException e) {
e.printStackTrace();
}
long startTime = start.getTime();
long endTime = end.getTime();
long betweenDays = (endTime - startTime) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
2、精确到秒,向下取值文章来源:https://www.toymoban.com/news/detail-503872.html
/**
* 获取两个日期间的天数,精确到秒,向下取值
* @param start
* @param end
* @return
*/
public static int daysBetween(Date start, Date end) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
start = sdf.parse(sdf.format(start));
end = sdf.parse(sdf.format(end));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(start);
long startTime = cal.getTimeInMillis();
cal.setTime(end);
long endTime = cal.getTimeInMillis();
long betweenDays = (endTime - startTime) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
如有问题,欢迎指正~ 如能帮到您,欢迎点赞收藏~文章来源地址https://www.toymoban.com/news/detail-503872.html
到了这里,关于java计算两个日期间的天数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!