1

I set Today is Feb 28 2014 and i using the code :

cal.add(Calendar.MONTH, 1);

The output is Mar 28 2014

But i want to show Mar 31 2014

what should i do ?

danielsa
  • 25
  • 3
  • 1
    What is the logic behind output you want to show? Do you want always to move date to next month and set day to last one of that month? If original date would be `Feb 18 2014` should result also be `Mar 31 2014`? – Pshemo Feb 23 '15 at 04:22

1 Answers1

2

You can move date to next month, and then set its day to max value for current month

cal.add(Calendar.MONTH, 1);//move date to next month
cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH));
  //set          day               to max value for current month
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    But only set to last day of month if, before adding a month to the date, this evaluates to true: `boolean isLastDay = cal.get(Calendar.DAY_OF_MONTH) == cal.getMaximum(Calendar.DAY_OF_MONTH);` – gknicker Feb 23 '15 at 05:23