Write a java program to find number of days in a month

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.

The first argument corresponds to the year and the second argument corresponds to the month code. The method returns an integer corresponding to the number of days in the month.

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

Find the number of days in a month in Java.

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;
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

17 + twenty =