public static void main(String[] args) throws ParseException {
System.out.println(TimeUtil.dayOfYear(1992, 12, 31));
System.out.println(TimeUtil.dayOfYear(2016, 3, 14));
JDateTime time = new JDateTime(new Date());
JDateTime time2 = new JDateTime(new SimpleDateFormat("yyyyMMdd").parse("20160310"));
System.out.println(time2.daysBetween(time));
}
daysBetween源码分析
/**
* Calculates the number of days between two dates. Returned value is always positive.
*/
public int daysBetween(JulianDateStamp otherDate) {
int difference = daysSpan(otherDate);
return difference >= 0 ? difference : -difference;
}
/**
* Returns span between two days. Returned value may be positive (when this date
* is after the provided one) or negative (when comparing to future date).
*/
public int daysSpan(JulianDateStamp otherDate) {
int now = getJulianDayNumber();
int then = otherDate.getJulianDayNumber();
return now - then;
}
/**
* Returns JDN. Note that JDN is not equal to {@link #integer}. It is calculated by
* rounding to the nearest integer.
*/
public int getJulianDayNumber() {
if (fraction >= 0.5) {
return integer + 1;
}
return integer;
}