Home > Java > javaTutorial > How to find the number of days in a certain month of a specific year in Java?

How to find the number of days in a certain month of a specific year in Java?

WBOY
Release: 2023-08-25 11:09:13
forward
1013 people have browsed it

How to find the number of days in a certain month of a specific year in Java?

A GregorianCalendar is a concrete subclass of Calendar class and it provides the standard calendar system used by most of the world. In Java, this GregorianCalendar can handle both the Gregorian calendar as well as Julian calendar. We can determine or find the number of days in a month of a particular year by using the getActualMaximum() method of GregorianCalendar class. This method returns the maximum value that the GregorianCalendar field can have. The parameter can be any field of a Calendar class.

Syntax

public int getActualMaximum(int field)
Copy after login

Example

import java.util.*;
public class NoOfDaysInAMonthOfAYearTest {
   public static void main(String []args) {
      for (int i = 2000; i < 2018; i++) {
         Calendar calendar = new GregorianCalendar(i, Calendar.FEBRUARY, 1);
         int numberOfDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
         System.out.println("February " + i + ": " + numberOfDays + " days");
      }
   }
}
Copy after login

输出

February 2000: 29 days
February 2001: 28 days
February 2002: 28 days
February 2003: 28 days
February 2004: 29 days
February 2005: 28 days
February 2006: 28 days
February 2007: 28 days
February 2008: 29 days
February 2009: 28 days
February 2010: 28 days
February 2011: 28 days
February 2012: 29 days
February 2013: 28 days
February 2014: 28 days
February 2015: 28 days
February 2016: 29 days
February 2017: 28 days
Copy after login

The above is the detailed content of How to find the number of days in a certain month of a specific year in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template