比如 4.1日---8.2日 就是 4 个月零 2 天
按照我的理解,2018-04-01
和 2018-08-02
应该是相差 4 个月零 1 天。
然后这是按照我的理解写出来的代码(基于 Java8):
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateDiff {
static int[] getDiff(LocalDate start, LocalDate end) {
if (!start.isBefore(end)) {
throw new IllegalArgumentException("Start must not be before end.");
}
int year1 = start.getYear();
int month1 = start.getMonthValue();
int day1 = start.getDayOfMonth();
int year2 = end.getYear();
int month2 = end.getMonthValue();
int day2 = end.getDayOfMonth();
int yearDiff = year2 - year1; // yearDiff >= 0
int monthDiff = month2 - month1;
int dayDiff = day2 - day1;
if (dayDiff < 0) {
LocalDate endMinusOneMonth = end.minusMonths(1); // end 的上一个月
int monthDays = endMinusOneMonth.lengthOfMonth(); // 该月的天数
dayDiff += monthDays; // 用上一个月的天数补上这个月差掉的日子
if (monthDiff > 0) { // eg. start is 2018-04-03, end is 2018-08-01
monthDiff--;
} else { // eg. start is 2018-04-03, end is 2019-02-01
monthDiff += 11;
yearDiff--;
}
}
int[] diff = new int[2];
diff[0] = yearDiff * 12 + monthDiff;
diff[1] = dayDiff;
return diff;
}
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2018, 4, 3);
LocalDate[] endDates = {
LocalDate.of(2018, 4, 5),
LocalDate.of(2018, 10, 6),
LocalDate.of(2019, 4, 5),
LocalDate.of(2019, 10, 6),
LocalDate.of(2019, 3, 3),
LocalDate.of(2019, 3, 1),
LocalDate.of(2019, 2, 1),
LocalDate.of(2019, 2, 2),
};
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for (LocalDate end : endDates) {
int[] diff = getDiff(startDate, end);
System.out.printf("%s - %s = %2d 个月零 %-2d 天\n",
formatter.format(end), formatter.format(startDate), diff[0], diff[1]);
}
}
}
运行结果:
刚发现原来 Java8 已经提供了解决这个问题 API,所以 getDiff
方法可以简化为:
static int[] getDiff(LocalDate start, LocalDate end) {
if (!start.isBefore(end)) {
throw new IllegalArgumentException("Start must not be before end.");
}
Period period = Period.between(start, end);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
return new int[] {years * 12 + months, days};
}
3 回答2.2k 阅读✓ 已解决
3 回答3.7k 阅读✓ 已解决
8 回答2.9k 阅读
4 回答2.3k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
3 回答1.5k 阅读✓ 已解决
1 回答1.9k 阅读✓ 已解决
可以利用
joda-time
对时间进行方便的操作。利用其中的
Interval
和Period
类即可完成你的需求。