Slim
Slim

Reputation: 1744

Calendar trouble - can't get the correct days

I have this code inside of a loop:

        Calendar cal = Calendar.getInstance();
        cal.setTime(row.getCell(0).getDateCellValue());
        ProdCalendar currentCal = DefaultProdCalendar.get();    
        int workingDaysBefore = 0;
        int workingDaysAfter = 0;       
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();    
        end.setTime(row.getCell(0).getDateCellValue());                                  
        start.add(Calendar.MONTH,-1);

        // DAYS OF PREV MONTH
        int pDate = end.get(Calendar.DAY_OF_MONTH);
        int allDaysThisMonth = end.getActualMaximum(Calendar.DAY_OF_MONTH);
        int allDaysPrevMonth = start.getActualMaximum(Calendar.DAY_OF_MONTH);

Where row.getCell(0).getDateCellValue() is a date comming form an xls file in this format: 15/08/13

The problem I'm facing is that this line: int allDaysPrevMonth = start.getActualMaximum(Calendar.DAY_OF_MONTH); is always returning 30

Why is the start Calendar always returning 30? I'm pretty sure that I'm missing something really small, but I'm unable to spot it.

Upvotes: 2

Views: 662

Answers (3)

ice
ice

Reputation: 124

Calendar start = Calendar.getInstance();

here,you get the start is current month -- December. then,you do noting with it but go back just one month,here :start.add(Calendar.MONTH,-1); finally,you getActualMaximum of "start"...of cauz, there is 30 the maxday in november。。。

Upvotes: 1

Rahul
Rahul

Reputation: 45070

If you look at the docs of Calendar#getActualMaximum(field),

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar. For example, the actual maximum value of the MONTH field is 12 in some years, and 13 in other years in the Hebrew calendar system.

In your case you've given DAY_OF_MONTH, which returns the maximum value the DAY_OF_MONTH field can have for the given month.

For example,

Calendar start = Calendar.getInstance(); // Be default, its today, December
System.out.println(start.getActualMaximum(Calendar.DAY_OF_MONTH)); // prints 31, as December as 31 days

And you change the month, then

Calendar start = Calendar.getInstance(); // Be default, its today, dec.
start.set(Calendar.MONTH, 10); // Month is set to November
System.out.println(start.getActualMaximum(Calendar.DAY_OF_MONTH)); // prints 30, as November as 30 days

You always get 30 because, the current month is December and when you do start.add(Calendar.MONTH,-1);, it sets the current month to November, and as explained above, since November has 30 days, you get 30 always.

Upvotes: 1

Zhedar
Zhedar

Reputation: 3510

You may want to set the time of your start like start.setTime(row.getCell(0).getDateCellValue()); and after that go 1 month back with start.add(Calendar.MONTH,-1);.
Otherwise you take the current month (which is decembre) as of today and get the previous month. Novembre has 30 days so getActualMaximum returns 30.
What you do is that you`re setting an explicit end date, but not that the corresponding start date. This causes your start date to be after your end date, which surely isn't your desired effect.

Upvotes: 2

Related Questions