tl;dr
Use the modern java.time classes that supplant the troublesome old legacy date-time classes.
YearMonth.from(
LocalDate.of( 2018 , Month.MAY , 31 )
).lengthOfMonth()
31
LocalDate.of( 2018 , Month.MAY , 31 )
.getMonthValue()
5
LocalDate.of( 2018 , Month.MAY , 31 )
.getYear()
2018
java.time
Your Question does not provide enough detail to diagnose the issue. But the question is moot, with a better alternative approach available now.
The troublesome Calendar
class is now legacy, replaced by the java.time classes.
The modern approach to representing a month uses the YearMonth
class.
YearMonth ym = YearMonth.of( 2018 , Month.MAY ) ;
Or get a YearMonth
from a date, a LocalDate
object.
LocalDate ld = LocalDate.of( 2018 , Month.MAY , 31 ) ;
YearMonth ym = YearMonth.from( ld ) ;
Interrogate for the length of the month.
int lengthOfMonth = ym.lengthOfMonth() ;
31
The LocalDate
can give you numbers for its day-of-month and its year.
int dayOfMonth = ld.getMonthValue() ;
int year = ld.getYear() ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?