找出2个日期之间的天数差异以及每个月的天数

时间:2013-10-25 09:17:27

标签: java date

我可以使用这种方法帮助我尝试制作。我有一个问题对象,它有一个目标日期,我需要找出这个问题与今天的日期相比,按月分割/拆分的天数。

想象一下这种情况: 可以说今天的日期 05-02-2013

ID  Target date
P1  02-02-2013
P2  27-01-2013
P3  26-01-2013
P4  05-12-2012

这意味着每个问题都是接下来几个月的这么多天:

    DEC JAN FEB
P1          3
P2      4   5
P3      5   5
P4  26  31  5

问题不能超过12个月。

现在我需要一种方法来对存储月份名称和晚期总数的这些数字求和。如果目标月份和现在月份相同,那么这是一个简单的案例,因为我可以减去日期和存储月份,但是当不是这样时该怎么办?我有以下代码:

List<Problem> problems = problemQuery.getResultList(); //Problems list is already filtered and contain only late problems. 

Calendar now = Calendar.getInstance();
Calendar before = Calendar.getInstance();
Map<Integer, Integer> newMap = new TreeMap<Integer, Integer>(); //map that contains month number and daysLateCount

for (Problem p : problems) {
    before.setTime(p.getTarget_date());
    int nowMonth = now.get(Calendar.MONTH);
    int beforeMonth = before.get(Calendar.MONTH);
    if (beforeMonth == nowMonth) { //easy case when both dates have same month
        int result = now.get(Calendar.DAY_OF_MONTH) - before.get(Calendar.DAY_OF_MONTH);
        if (newMap.containsKey(nowMonth)) {
            int newLateDaysValue = newMap.get(nowMonth)+result; //get old result and add the new
            newMap.put(nowMonth, newLateDaysValue);
        }                   
        else {
            newMap.put(nowMonth, result);
        }
    }
    else {
                         //What to do here???
    }
}

也许我甚至可以跳过if-else子句并制作一个可以处理这两种情况的算法?我不知道请帮忙:)。

4 个答案:

答案 0 :(得分:3)

最好的方法是使用Joda Time库:http://www.joda.org/joda-time/

Java日期/时间API对于此类目的而言并不是非常好用。

答案 1 :(得分:1)

如果只知道二月份的天数,那么这一年是必要的。

    for (Problem p : problems) {
        int nowYear = now.get(Calendar.YEAR);
        int nowMonth = now.get(Calendar.MONTH);
        int nowDay = now.get(Calendar.DAY_OF_MONTH);

        before.setTime(p.getTarget_date());
        int beforeYear = before.get(Calendar.YEAR);
        int beforeMonth = before.get(Calendar.MONTH);
        int beforeDay = before.get(Calendar.DAY_OF_MONTH);
        while (beforeYear < nowYear || beforeMonth < nowMonth) {
            int daysInMonth =
                before.getActualMaximum(Calendar.DAY_OF_MONTH);
            int result = daysInMonth - beforeDay;

            Integer oldLateDaysValue = newMap.get(beforeMonth);
            newMap.put(beforeMonth,
                oldLateDaysValue == null ?
                    result : (oldLateDaysValue + result));

            // For all subsequent months, calculate using entire month.
            beforeDay = 0;

            before.add(Calendar.MONTH, 1);
            beforeYear = before.get(Calendar.YEAR);
            beforeMonth = before.get(Calendar.MONTH);
        }

        int result = nowDay - beforeDay;

        Integer oldLateDaysValue = newMap.get(beforeMonth);
        newMap.put(beforeMonth,
            oldLateDaysValue == null ?
                result : (oldLateDaysValue + result));
    }

    System.out.println(newMap);
}

答案 2 :(得分:1)

我认为有一个相对简单的解决方案,算法如下:

import java.util.Calendar;

public class test {

    public static void main(String[] args){

        Calendar today = Calendar.getInstance();
        Calendar problemDate = Calendar.getInstance();

        today.set(2013, 01, 05);
        problemDate.set(2012, 11, 05);
        System.out.println(today.getTime());
        System.out.println(problemDate.getTime());

        // This might need further validation to make sure today >= problemDate
        int diffYear = today.get(Calendar.YEAR) - problemDate.get(Calendar.YEAR);
        int differenceInMonths = diffYear * 12 + today.get(Calendar.MONTH) - problemDate.get(Calendar.MONTH);
        //int differenceInMonths = today.get(Calendar.MONTH) - problemDate.get(Calendar.MONTH);

        for(int i = 0; i <= differenceInMonths; i++) {
          int daysDifference;

          if (differenceInMonths == 0) {
             daysDifference = today.get(Calendar.DAY_OF_MONTH) - problemDate.get(Calendar.DAY_OF_MONTH);
          } else {
            if ( i == 0) { // first month
              daysDifference = problemDate.getActualMaximum(Calendar.DAY_OF_MONTH) - problemDate.get(Calendar.DAY_OF_MONTH);
            }
            else if( i == differenceInMonths ) { // last month
              daysDifference = today.get(Calendar.DAY_OF_MONTH);
            }
            else {
              Calendar cal= Calendar.getInstance();
              cal.set(Calendar.MONTH, problemDate.get(Calendar.MONTH) + i);
              daysDifference = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            } 
                  }

          System.out.println(daysDifference);
        }
    }
}

哪个输出:

Tue Feb 05 14:35:43 GMT 2013
Wed Dec 05 14:35:43 GMT 2012
26
31
5

你应该能够将它包装到你的代码中,并且相当容易地循环,并且还可以删除print语句以插入到你拥有的任何数据结构中。

答案 3 :(得分:1)

使用Joda-Time的解决方案:

LocalDate today = new LocalDate(2013, 2, 5);
LocalDate targetDate = new LocalDate(2012, 12, 5); // example with target date P4

LocalDate begin = targetDate;
LocalDate end = begin.dayOfMonth().withMaximumValue();

while (end.isBefore(today)) {
    Days days = Days.daysBetween(begin, end);
    if (days.getDays() > 0) {
        System.out.println(end.monthOfYear().getAsText() + ": " + days.getDays());
    }

    begin = end;
    end = begin.plusDays(1).dayOfMonth().withMaximumValue();
}

end = today;
Days days = Days.daysBetween(begin, end);
if (days.getDays() > 0) {
    System.out.println(end.monthOfYear().getAsText() + ": " + days.getDays());
}

打印以下结果,例如:目标日期P4:

  12月26日   1月:31日   二月:5