Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect months with 31 days

Is there an analogous form of the following code:

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

Or must it be:

if(month == 4 || month == 6 etc...)
{
  do something;
}

I am trying to write an if statement that checks if this month has more than 31 days.

EDIT

I guess the real problem is I undersand some of what I am taught but every time I try to use the sun website about java it just confuses me. My question is if I get a month in from a user and a day and I put it into a MM/dd format and evaluate it then is there an easier way to check if the month and the day is valid and after I check it for being valid I can either print the MM/dd in the format that I have. If it is not valid Print a line that says Invalid month or day.

like image 563
daddycardona Avatar asked Nov 27 '09 00:11

daddycardona


2 Answers

if( 0x0A50 & (1<<month) != 0 )

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

like image 134
irreputable Avatar answered Nov 11 '22 09:11

irreputable


If you're using C or Java, you can do this:

switch (month) {
  case 4:
  case 6:
  case 9:
  case 11:
    do something;
    break;
}

In some languages, you could even write case 4,6,9,11:. Other possibilities would be to create an array [4,6,9,11], some functional languages should allow something like if month in [4,6,9,11] do something;

As Lior said, it depends on the language.

EDIT: By the way, you could also do this (just for fun, bad code because not readable):

if ((abs(month-5) == 1) || (abs(month-10) == 1)) do_something;
like image 12
schnaader Avatar answered Nov 11 '22 09:11

schnaader