2013-10-25 95 views
1

我可以使用這個方法,我試圖做一些幫助。我有一個問題對象,它有一個目標日期,我需要找出這個問題在多少天內被分裂/分裂的時間比今天的日期還要多。找到兩個日期之間的天數差異,並在每個月有多少天

形象這種情況: 可以說今天的日期是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子句,並制定一個算法,可以處理這兩種情況?我不知道,請幫助:)

+0

有一個問題是超過12月中下旬的可能性?具體來說,最近幾個月可能包括兩個或更多Januarys,兩個或更多Februarys等? – VGR

+0

我應該包括在我的問題:)。沒有問題不能超過12個月 –

回答

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 

你應該能包住此成你的代碼和相當容易的循環,並且還可以刪除打印語句以插入你擁有的任何數據結構。

3

最好的辦法是使用約達時間庫:http://www.joda.org/joda-time/

的Java日期/時間API不是很好的和有用的用於上述目的。

+0

在我發佈我的問題之前,我確實看了一下喬達時間,但我仍然沒有一個想法如何使用它來解決我的問題:( –

+0

專門看看'Months.monthsBetween'和'Days.daysBetween'方法。 –

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); 
} 
+0

+1,用於改進我的地圖代碼:) 但是,爲什麼要在beforeDay中添加+1?這在第一個月給了我一個錯誤的結果。例如,我在2013年5月8日的目標日期中遇到問題,它將打印出問題是在5月下旬的24天后 –

+0

您說得對。我試圖說明31-1在一個月內不給31天的事實。我已通過將'beforeDay'零並刪除+1來更新它,這應該會給出正確的結果。 – VGR

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:

十二月:26
1月:31
2月:5

相關問題