如何在Java中找到特定年份的某个月份的天数?

2023年 8月 28日 91.5k 0

如何在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.

public int getActualMaximum(int field)

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");
}
}
}

登录后复制

输出

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中找到特定年份的某个月份的天数?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论