Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

find the no of days in a month based on year in java

import java.util.*;
 
import java.time.YearMonth;
 
public class GetNumberOfDaysInMonthJava {
 
    public static void main(String[] args) {
        // Get number of days in given month of the year
      	int year,month;
      	Scanner input = new Scanner(System.in);
      	year = input.nextInt();
      	month = input.nextInt();
        int numberOfDaysInMonth1 = getNumberOfDaysInMonth(year, month);
        System.out.println("Number of days in Feb 2019: "+numberOfDaysInMonth1);
 
        int numberOfDaysInMonth2 = getNumberOfDaysInMonth(2020, 2);
        System.out.println("Number of days in Feb 2020: "+numberOfDaysInMonth2);
 
    }
 
    // Method to get number of days in month
    public static int getNumberOfDaysInMonth(int year,int month)
    {
        YearMonth yearMonthObject = YearMonth.of(year, month);
        int daysInMonth = yearMonthObject.lengthOfMonth();
        return daysInMonth;
    }
}
 
Source by java2blog.com #
 
PREVIOUS NEXT
Tagged: #find #days #month #based #year #java
ADD COMMENT
Topic
Name
4+2 =