首页 > 编程 > Java > 正文

java获取日期之间天数的方法

2019-11-26 15:04:13
字体:
来源:转载
供稿:网友

本文实例讲述了java获取日期之间天数的方法。分享给大家供大家参考。具体实现方法如下:

private int daysBetween(Date now, Date returnDate) {  Calendar cNow = Calendar.getInstance();  Calendar cReturnDate = Calendar.getInstance();  cNow.setTime(now);  cReturnDate.setTime(returnDate);  setTimeToMidnight(cNow);  setTimeToMidnight(cReturnDate);  long todayMs = cNow.getTimeInMillis();  long returnMs = cReturnDate.getTimeInMillis();  long intervalMs = todayMs - returnMs;  return millisecondsToDays(intervalMs);}private int millisecondsToDays(long intervalMs) {  return (int) (intervalMs / (1000 * 86400));}private void setTimeToMidnight(Calendar calendar) {  calendar.set(Calendar.HOUR_OF_DAY, 0);  calendar.set(Calendar.MINUTE, 0);  calendar.set(Calendar.SECOND, 0);}

希望本文所述对大家的java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表