tl;dr
EnumSet.of( Month.JANUARY , Month.MARCH , Month.MAY , Month.JULY , Month.AUGUST , Month.OCTOBER , Month.DECEMBER )
.contains( Month.from( LocalDate.now() ) )
java.time
The modern approach uses the java.time classes that supplant the troublesome old date-time classes.
The Month
enum defines an object for each month of the year.
EnumSet<Month> thirtyOneDayMonths = EnumSet.noneOf( Month.class ) ;
for( Month month : Month.values() ) {
if( month.maxLength() == 31 ) {
thirtyOneDayMonths.add( month ) ;
}
}
See if the current month is in that collection of months that have 31 days.
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
Month currentMonth = Month.from( today ) ;
if( thirtyOneDayMonths.contains( currentMonth ) ) {
…
}
Or you could just interrogate today s date.
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
.lengthOfMonth()
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?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.