GregorianCalendar是Calendar类的具体子类,它提供了世界上大多数人使用的标准日历系统。在 Java 中,这个 GregorianCalendar 可以处理 Gregorian 日历以及 Julian 日历。我们可以使用 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
以上是如何在Java中找到特定年份的某个月份的天数?的详细内容。更多信息请关注PHP中文网其他相关文章!