Detect months with 31 days

前端 未结 19 1603
南旧
南旧 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:19

    For dates I use Joda Time mentioned earlier, but I understand if it's not applicable for you. If you just want it to look nice, you can first define a list with values that you're interested in and then check if your month is in that list:

    // This should be a field in a class
    // Make it immutable
    public static final List LONGEST_MONTHS = 
            Collections.immutableList(Arrays.asList(4,6,9,11));
    
    // Somewhere in your code
    if(LONGEST_MONTHS.contains(month)) {
        doSomething();
    }
    

提交回复
热议问题