如何在Java中查找特定年份的一个月中的天数?
一个GregorianCalendar的 是一个具体子类日历 类,它提供了世界上大多数国家使用的标准日历系统。在Java中,此GregorianCalendar 可以处理公历 和儒略 历。我们可以使用GregorianCalendar 类的getActualMaximum() 方法确定或找到特定年份一个月中的天数。此方法返回GregorianCalendar字段可以具有的最大值。该参数可以是Calendar 类的任何字段。
语法
public int getActualMaximum(int field)
示例
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"); } } }
输出结果
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