1

We are using the below code snippet to get number of days for the provided month and year. For 02 and 2011, It returns the no of days as 31 ( which is not the case). for 02 and 2016, it returns the no of days as 29.

Any clues.

package Processes.BSAInvoiceInquiry.ExternalCall.PaymentStatusInquiry;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class PaymentStatusInquiryJavaCode {

    protected int year = 0;
    protected int month = 0;
    protected int days = 0;

    public void invoke() throws Exception {

        PaymentStatusInquiryJavaCode a = new PaymentStatusInquiryJavaCode();

        System.out.println("Year  " + year);
        System.out.println("Month  " + month);

        Calendar calObj = new GregorianCalendar();
        calObj.set(Calendar.YEAR, year);
        calObj.set(Calendar.MONTH, month - 1);
        System.out.println("Month  " + Calendar.MONTH);
        int numDays = calObj.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("No of the days in the month is   " + numDays);
        days = numDays;

    }
}
zlakad
  • 1,314
  • 1
  • 9
  • 16
Abdul Kader
  • 5,781
  • 4
  • 22
  • 40
  • For 02 and 2011, It returns the no of days as 31 ( which is not the case). – Abdul Kader Apr 29 '18 at 11:04
  • 1
    You should absolutely look into Java 8's java.time package. Rock solid comfortable immutable tools. – Dreamspace President Apr 29 '18 at 11:20
  • @DreamspacePresident our tool is still dependent on java 1.7 – Abdul Kader Apr 29 '18 at 11:24
  • 2
    No big problem @AbdulKader. `java.time` has been backported to Java 6 and 7. Get [ThreeTen Backport](http://www.threeten.org/threetenbp/), add it to your project and start using the modern Java date and time API. And enjoy! With the trouble you’ve experienced with the old-fashioned `Calendar` class this will be a great relief. – Ole V.V. Apr 29 '18 at 12:04
  • @AbdulKader I have written [a new answer to the linked question](https://stackoverflow.com/a/50088210/5772882). The paragraph “If you only needed a count of days in some month…” is for you. Please take a look. – Ole V.V. Apr 29 '18 at 15:30

3 Answers3

3

This is just another unexpected behavior of Calendar, see this, you can fix it by clear after the creation:

Calendar calendar = new GregorianCalendar();
calendar.clear();
calendar.set(Calendar.YEAR, 2011);
calendar.set(Calendar.MONTH, 1);
System.out.println(calendar.getActualMaximum(calendar.DAY_OF_MONTH)); //28

The use of outdated Calendar should be avoided. In java8, this can be done by:

YearMonth yearMonth = YearMonth.of(2011, 2);
int lengthOfMonth = yearMonth.lengthOfMonth();
System.out.println(lengthOfMonth); //28
xingbin
  • 27,410
  • 9
  • 53
  • 103
2

To complete user6690200's answer it returns 29 for 2016 because it's the 29th today and 2016 was a leap year and had a 29th of february. 2011 wasn't a leap year so it actually returns the number for the next month(March which has 31 days).

Oleg
  • 6,124
  • 2
  • 23
  • 40
0

try

// month 1 based    
new Calendar.Builder().setDate(year, month-1, 1).build().getActualMaximum(DAY_OF_MONTH)

the problem is none

calObj.set(DAY_OF_MONTH, 1);
Giacomo
  • 94
  • 6