Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is a relatively simple solution to this, the algorithm is as follows:</p> <pre><code>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 &gt;= 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 &lt;= 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); } } } </code></pre> <p>Which outputs:</p> <pre><code>Tue Feb 05 14:35:43 GMT 2013 Wed Dec 05 14:35:43 GMT 2012 26 31 5 </code></pre> <p>You should be able to wrap this up into your code, and in a loop fairly easily, and also remove the print statements to insert into whatever data structure you have.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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