1

我有 2 个日期作为输入。我想数一数他们之间的岁月。当不到 1 年时,我希望得到一个双倍(即 0.xyz)作为输出。我尝试了以下功能:

private double getYearCount(Date startDate, Date endDate)
{
        long millisecondCount = endDate.getTime() - startDate.getTime();
        long yearMillisecondCount = 1000L * 60L * 60L * 24L * 365L;
        return millisecondCount / (double) yearMillisecondCount;
}

但我在那里假设一年是 365 天。所以闰年不被重视。这两个日期可能有不同的年份(例如它们可能是01.01.198707.01.2013)。

我怎样才能解决它,让我也注意到闰年?

4

2 回答 2

0

只需使用正确的时间一年。也就是 365.2425 天。

但是,不建议这样做。使用时间/日期库来确定这两年的年份,然后减去它们。

为两个日期初始化一个 GregorianCalendar,并获取年份。

于 2013-01-20T20:59:49.673 回答
0

我写了一个返回日期范围内的年数和天数的方法。正如James K. Polk在他的评论中提到的那样,您可以将天除以 365、365.2425 或 366 以获得一年的小数部分。

以下是测试结果。

From 01 Jan 1987 to 01 Jul 2013 is an interval of 26 years and 182 days.
From 01 Jul 2012 to 01 Jul 2013 is an interval of 1 years and 1 days.
From 01 Jul 2012 to 31 Jul 2012 is an interval of 0 years and 31 days.
From 01 Apr 2012 to 31 Dec 2012 is an interval of 0 years and 275 days.

这是代码。基本上,我是从开始日期到结束日期,逐年计算。

此代码适用于 Java 6、Java 7 及更高版本。

package com.ggl.testing;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateRangeCalculator {

    private static final SimpleDateFormat format = new SimpleDateFormat(
            "dd MMM yyyy");

    public static void main(String[] args) {
        DateRangeCalculator dateRangeCalculator = new DateRangeCalculator();
        calculateDateInterval(dateRangeCalculator, "01 Jan 1987", "01 Jul 2013");
        calculateDateInterval(dateRangeCalculator, "01 Jul 2012", "01 Jul 2013");
        calculateDateInterval(dateRangeCalculator, "01 Jul 2012", "31 Jul 2012");
        calculateDateInterval(dateRangeCalculator, "01 Apr 2012", "31 Dec 2012");
    }

    private static void calculateDateInterval(
            DateRangeCalculator dateRangeCalculator, String startDateString,
            String endDateString) {
        try {
            Date startDate = format.parse(startDateString);
            Date endDate = format.parse(endDateString);
            DateInterval dateInterval = dateRangeCalculator.getDateInterval(
                    startDate, endDate);
            System.out.println("From " + format.format(startDate) + " to "
                    + format.format(endDate) + " is an interval of "
                    + dateInterval.getYears() + " years and "
                    + dateInterval.getDays() + " days.");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public DateInterval getDateInterval(Date startDate, Date endDate) {
        Calendar startCalendar = createCalendar(startDate);
        Calendar endCalendar = createCalendar(endDate);

        int years = -1;
        while (!startCalendar.after(endCalendar)) {
            startCalendar.add(Calendar.YEAR, 1);
            years++;
        }

        startCalendar.add(Calendar.YEAR, -1);

        int days = 0;
        while (!startCalendar.after(endCalendar)) {
            startCalendar.add(Calendar.DAY_OF_YEAR, 1);
            days++;
        }

        return new DateInterval(years, days);
    }

    private Calendar createCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public class DateInterval {
        private final int years;
        private final int days;

        public DateInterval(int years, int days) {
            this.years = years;
            this.days = days;
        }

        public int getYears() {
            return years;
        }

        public int getDays() {
            return days;
        }

    }

}
于 2016-03-09T12:29:17.823 回答