java

How to get the number of days in a particular month of a particular year in Java

Especially in English-speaking countries, the use of the calendar is very common. The calculation and conversion are done with the class LocalDate in Java 8 and with Calendar in Java before version 8.

Let’s see how we can get the number of days in a particular month of a particular year in java by using the Calendar class:

//Get the number of days in a month 
public static int getDaysOfMonth(int year, int month)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month-1);
    int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    
    return days_of_month;
}
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More [st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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