How to get days in a month in Java

In this article, we will show you how to get days in a month.

package com.kruders;
 
import java.util.Calendar;
 
public class GetDaysInMonth {
    public static void main(String[] args) {
        //display number of days of current month
        Calendar calendar = Calendar.getInstance();
        System.out.println("Days in Current Month " + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
         
        //display number of days in February
        calendar.set(Calendar.MONTH,Calendar.FEBRUARY);
        System.out.println("Days in February Month " + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    }
}

When you run the above example you’ll get an output like:

Days in Current Month 31
Days in February Month 29


No comments yet.

Leave a Reply