Detect months with 31 days

前端 未结 19 1598
南旧
南旧 2020-12-18 01:53

Is there an analogous form of the following code:

if(month == 4,6,9,11)
{
  do something;
}

Or must it be:

if(month == 4 ||         


        
相关标签:
19条回答
  • 2020-12-18 02:39

    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?

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    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.

    0 讨论(0)
  • 2020-12-18 02:39

    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);
    
    0 讨论(0)
  • 2020-12-18 02:40
    if( 0x0A50 & (1<<month) != 0 )
    

    dude, this is ridiculous. (month==4||month==6||month==9||month==11) is perfectly ok.

    0 讨论(0)
  • 2020-12-18 02:41

    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));
        }
    }
    
    0 讨论(0)
  • 2020-12-18 02:41

    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;
       } 
    
    0 讨论(0)
  • 2020-12-18 02:44

    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

    0 讨论(0)
提交回复
热议问题