# Method to determine number of days in a month

Page 1 of 1

## 2 Replies - 2330 Views - Last Post: 23 October 2009 - 08:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133875&amp;s=da4cf689c14cc099b65aef12eed3f221&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 quyetnguyen

Reputation: 0
• Posts: 8
• Joined: 20-October 09

# Method to determine number of days in a month

Posted 23 October 2009 - 05:08 AM

This is exercise ,Who can help me test ? thanks you.

4. Problem 04: Day of Month
- Class-name: cDayOfMonth_<mssv>
- Requirment: print out the number days of a month.
- Input: Month (an integer) and Year (integer) from Console
- Output:
o If this month is not valid (không hợp lệ) then print out:
<month>/<year> is invalid year.
o Else: print out the number days of this month
<month>/<year> has <number-day> days.
- Note: becarful with leap year.
- Example:
o Ex1:
Input:
• Month: 13
• Year: 2009
Output: 13/2009 is invalid month.
o Ex2:
Input:
• Month: 8
• Year: 2010
Output: 8/2010 has 31 days.
o Ex3:
Input:
• Month: 2
• Year: 1200
Output: 2/1200 has 29 days.

This is code of myself.
```import java.util.*;
public class test4 {	  /** 	 * @param args 	 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int month, year;
System.out.print("Input moth month : ");
month=in.nextInt();
System.out.print("Input year year: ");
year=in.nextInt();
if(month<0&&month<=13){
System.out.print(month+"/"+year+" is invalid year ");
}
else{ 			if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ 				System.out.print(month+"/"+year+" has 31");
}
if(month==4||month==6||month==9||month==11){
System.out.print(month+"/"+year+" has 30 ");
}
if(month==2&&(year%4==0&&year%100!=0||year%400==0))
{
System.out.print(month+"/"+year+" has is 28");
}
}
}
}
```

*Edited: to put a more describtive Topic Title

This post has been edited by pbl: 23 October 2009 - 08:51 PM

Is This A Good Question/Topic? 0

## Replies To: Method to determine number of days in a month

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,401
• Joined: 27-December 08

## Re: Method to determine number of days in a month

Posted 23 October 2009 - 06:31 AM

Please use proper indentation when posting your code. It is extremely hard to read. For your days, I'd use a switch statement like so:

```//Returns num days in month or -1 if invalid month
public int daysInMonth(int month, int year){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default: return -1;
..cases for months w/30 days and february..
}
}

```

This is easier to read than an if statement with 5 or 6 conditions.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Method to determine number of days in a month

Posted 23 October 2009 - 08:50 PM

And please respect rule #5 of this forum

Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"

"Help me" is kind of useless... and it is not the first time I told you