Note that there are some explanatory texts on larger screens.

plurals
  1. POfind days difference between 2 dates and how many days in each month
    primarykey
    data
    text
    <p>I could use some help with this method I'm trying to make. I have a Problem Object, which has a target date and i need to find out how many days this problem is late divided/split by months, compared to today's date.</p> <p>Image this situation: Lets say that today's date is <strong>05-02-2013</strong>.</p> <pre><code>ID Target date P1 02-02-2013 P2 27-01-2013 P3 26-01-2013 P4 05-12-2012 </code></pre> <p>This means that each problem is this many days late in the following months:</p> <pre><code> DEC JAN FEB P1 3 P2 4 5 P3 5 5 P4 26 31 5 </code></pre> <p>A problem can not be older than 12 months.</p> <p>Now i need a method to sum these numbers storing the month name and a summed number of late days. If the target month and now month are the same, its an easy case, because i can just substract the days and store the month, but what to do when its not the case? I have the following code:</p> <pre><code>List&lt;Problem&gt; problems = problemQuery.getResultList(); //Problems list is already filtered and contain only late problems. Calendar now = Calendar.getInstance(); Calendar before = Calendar.getInstance(); Map&lt;Integer, Integer&gt; newMap = new TreeMap&lt;Integer, Integer&gt;(); //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??? } } </code></pre> <p>Perhaps i could even skip the if-else clause and make an algorithm that could handle both cases? I don't know please help :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload