私が作ろうとしているこの方法で助けを借りることができます。目標日を持つ問題オブジェクトがあり、今日の日付と比較して、この問題が何日遅れているかを月で分割/分割する必要があります。
この状況をイメージしてください: 今日の日付が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 句をスキップして、両方のケースを処理できるアルゴリズムを作成することもできますか? わかりません 助けてください:)