这个问题已经在这里有一个答案:
- 计算两个Java日期实例之间的区别 45个回答
- 在Java计算日期/时间差重复] 17个答案
喜在日期格式两个日期,我如何得到天两者之间的区别?
Date date1;
Date date2 ;
int numberDays = ?
这个问题已经在这里有一个答案:
喜在日期格式两个日期,我如何得到天两者之间的区别?
Date date1;
Date date2 ;
int numberDays = ?
该建议是使用JodaTime API的日期:
import java.util.logging.Logger;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;
public class DatesInterval {
private final static Logger log = Logger.getLogger(DatesInterval.class.getName());
public static void main(String[] args) {
//creates a date 10 days ago in JodaTime
DateTime daysAgo10 = new DateTime().minusDays(10);
//today
DateTime today = new DateTime();
//create an interval in Joda
Interval interval = new Interval(daysAgo10.getMillis(), today.getMillis());
//than get the duration
Duration duration = interval.toDuration();
//now you can get what you want. As you can imagine you can get days, millis, whateaver you need.
log.info("Difference in days: " + duration.getStandardDays());
}
}
http://joda-time.sourceforge.net/
问候。