Issue with Gregorian Calendar [duplicate]
Asked Answered
B

3

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;

    }
}
Baxley answered 29/4, 2018 at 10:57 Comment(5)
For 02 and 2011, It returns the no of days as 31 ( which is not the case).Baxley
You should absolutely look into Java 8's java.time package. Rock solid comfortable immutable tools.Supercool
@DreamspacePresident our tool is still dependent on java 1.7Baxley
No big problem @AbdulKader. java.time has been backported to Java 6 and 7. Get ThreeTen Backport, 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.Profuse
@AbdulKader I have written a new answer to the linked question. The paragraph “If you only needed a count of days in some month…” is for you. Please take a look.Profuse
H
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
Horny answered 29/4, 2018 at 11:17 Comment(2)
@user66902200 didn't help , same issueBaxley
@AbdulKader Then please post your test code. The test code in the answer works fine.Horny
U
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).

Unopened answered 29/4, 2018 at 11:5 Comment(2)
ideally should it not be 28Baxley
Don't know, that's how they implemented it.Unopened
C
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);
Cocoon answered 29/4, 2018 at 11:35 Comment(2)
DAY_OF_MONTH is 1 based, 0 day is a day minusCocoon
2011:2-1:0 is 2011:1:31 then 31, 2016:2-1:0 is 2016:1:29 then 29Cocoon

© 2022 - 2024 — McMap. All rights reserved.