Given two inputs year and month (Month is coded as Jan=0, Feb=1, Mar=2 …), write a program to find out the total number of days in the given month for the given year.
Input and Output Format
- Input consists of 2 integers that correspond to the year and month code.
- The output consists of an integer that corresponds to the number of days in the month in the given year.
Sample Input:
2000
1
Sample Output:
29
Number of days in a month in Java
Following are the steps to find the number of days in a month in Java:
- Input year and month from the user.
- Pass it to the display() method.
- Inside the method, first, get the instance of the Calendar object.
- We will set the year and month to the calendar instance.
- Now, get the maximum days for the year and months specified.
- At last, return the number of days in a month.
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int month = sc.nextInt(); System.out.println(display(year, month)); } public static int display(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); int DAY_OF_MONTH = cal.getActualMaximum(cal.DAY_OF_MONTH); return DAY_OF_MONTH; } }
Note: In java.util.Calendar class, the month starts from 0 and not 1.
Output
Print Days in a month using if-else
Here, we will use the if-else statement. Following are the steps:
- Input month and year from the user.
- Pass them to the display() method.
- Inside the method, Declare and initialize variable no_of_days_in_month to 0.
- Check the input with months (like if it is 1 then it is January) and then assign a number of days to the no_of_days_in_month variable.
- At last, just return no_of_days_in_month.
package com.date; import java.util.Scanner; public class Practice2Application { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); int month = scanner.nextInt(); System.out.println(display(year,month)); } private static int display(int year, int month) { int no_of_days_in_month = 0; if(month==1){ no_of_days_in_month = 31; } else if(month==2){ if((year%400==0) || ((year%100!=0) && (year%4==0))){ no_of_days_in_month = 29; } else { no_of_days_in_month = 28; } } else if(month==3){ no_of_days_in_month = 31; } else if(month==4){ no_of_days_in_month = 30; } else if(month==5){ no_of_days_in_month = 31; } else if(month==6){ no_of_days_in_month = 30; } else if(month==7){ no_of_days_in_month = 31; } else if(month==8){ no_of_days_in_month = 31; } else if(month==9){ no_of_days_in_month = 30; } else if(month==10){ no_of_days_in_month = 31; } else if(month==11){ no_of_days_in_month = 30; } else if(month==12){ no_of_days_in_month = 31; } return no_of_days_in_month; } }
Output
Program to find days in a month using switch case
Following are the steps we shall see:
- Input month and year from the user.
- Pass them to the display() method.
- Inside the method, Declare and initialize variable no_of_days_in_month to 0.
- Use the switch case and check the input with months (like if it is 1 then it is January) and then assign a number of days to the no_of_days_in_month variable.
- At last, just return no_of_days_in_month.
package com.date; import java.util.Scanner; public class Practice3Application { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); int month = scanner.nextInt(); System.out.println(display(year, month)); } private static int display(int year, int month) { int no_of_days_in_month = 0; switch (month){ case 1: no_of_days_in_month = 31; break; case 2: if((year%400==0) || ((year%100!=0) && (year%4==0))){ no_of_days_in_month = 29; break; } else { no_of_days_in_month = 28; break; } case 3: no_of_days_in_month = 31; break; case 4: no_of_days_in_month = 30; break; case 5: no_of_days_in_month = 31; break; case 6: no_of_days_in_month = 30; break; case 7: no_of_days_in_month = 31; break; case 8: no_of_days_in_month = 31; break; case 9: no_of_days_in_month = 30; break; case 10: no_of_days_in_month = 31; break; case 11: no_of_days_in_month = 30; break; case 12: no_of_days_in_month = 31; break; } return no_of_days_in_month; } }
Output
Thus, this is how we can find out the number of days in a particular year & month.