Is there an analogous form of the following code:
if(month == 4,6,9,11)
{
do something;
}
Or must it be:
if(month == 4 ||
EnumSet.of( Month.JANUARY , Month.MARCH , Month.MAY , Month.JULY , Month.AUGUST , Month.OCTOBER , Month.DECEMBER )
.contains( Month.from( LocalDate.now() ) )
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()
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.
Move to first of next month and subtract a day.
Calendar cal=Calendar.getInstance();
cal.add(Calendar.MONTH,1);
cal.set(Calendar.DAY_OF_MONTH,1);
cal.add(Calendar.DATE, -1);
if( 0x0A50 & (1<<month) != 0 )
dude, this is ridiculous. (month==4||month==6||month==9||month==11)
is perfectly ok.
I think your code will be more self-documenting if you use the static constants built into Calendar (e.g., Calendar.JANUARY, etc.)
If you do this often - more than once - I'd recommend writing a method named has31Days() or isReportMonth() to do the check in one place.
UPDATE:
The important thing isn't the months that have 31 days - it's the business rule that tells you something about reports for those months.
I might write it like this (hope I got the months with 31 days right):
public class ReportEngine
{
public boolean isReportRequired(int month)
{
if ((month < Calendar.JANUARY) || (month > Calendar.DECEMBER))
throw new IllegalArgumentException("Invalid month: " + month);
// Reports are required for months with 31 days.
return ((month == Calendar.JANUARY) ||
(month == Calendar.MARCH) ||
(month == Calendar.MAY) ||
(month == Calendar.JULY) ||
(month == Calendar.AUGUST) ||
(month == Calendar.OCTOBER) ||
(month == Calendar.DECEMBER));
}
}
If you're checking if a month has more than 31 days, the simplest way to write it would be (in Java):
public static boolean hasMoreThan31Days(final int month) {
return false;
}
No question about dates and Java would be complete without mentioning Joda Time.
for(int i = DateTimeConstants.JANUARY; i <= DateTimeConstants.DECEMBER; i++) {
LocalDate localDate = new LocalDate().withMonthOfYear(i);
System.out.println(" " +localDate.toString("MMMM") + " - " + localDate.dayOfMonth().getMaximumValue());
}
January - 31
February - 29
March - 31
April - 30
May - 31
June - 30
July - 31
August - 31
September - 30
October - 31
November - 30
December - 31