A 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中文網其他相關文章!