在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  網(wǎng)絡(luò)安全/ Java 如何計(jì)算兩個日期相差幾個月零幾天?

Java 如何計(jì)算兩個日期相差幾個月零幾天?

比如 4.1日---8.2日 就是 4 個月零 2 天

回答
編輯回答
有點(diǎn)壞

可以利用joda-time對時間進(jìn)行方便的操作。

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.9</version>
</dependency>

利用其中的IntervalPeriod類即可完成你的需求。

Interval interval = new Interval(d1.getTime(), d2.getTime()); //d1,d2為Date類型
Period p = interval.toPeriod(); //得到相差的時間段
p.getDays()    //相差天數(shù)
p.getHours()   //相差小時
p.getMinutes() //相差分鐘
p.getSeconds() //相差秒
2017年2月24日 08:23
編輯回答
無標(biāo)題

按照我的理解,2018-04-012018-08-02 應(yīng)該是相差 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();  // 該月的天數(shù)

            dayDiff += monthDays;  // 用上一個月的天數(shù)補(bǔ)上這個月差掉的日子

            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]);
        }
    }
}

運(yùn)行結(jié)果:
運(yùn)行結(jié)果

剛發(fā)現(xiàn)原來 Java8 已經(jīng)提供了解決這個問題 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};
}
2017年1月9日 23:23