oracle+指定月份天数,特定年份的特定月份的天数?

Java 8及更高版本

@Warren M. Nocos。如果您尝试使用Java 8的新Date和Time API,则可以使用java.time.YearMonthclass。请参阅Oracle教程。

// Get the number of days in that month

YearMonth yearMonthObject = YearMonth.of(1999, 2);

int daysInMonth = yearMonthObject.lengthOfMonth(); //28

测试:在闰年中尝试一个月:

yearMonthObject = YearMonth.of(2000, 2);

daysInMonth = yearMonthObject.lengthOfMonth(); //29

Java 7及更早版本

创建日历,设置年份和月份并使用 getActualMaximum

int iYear = 1999;

int iMonth = Calendar.FEBRUARY; // 1 (months begin with 0)

int iDay = 1;

// Create a calendar object and set year and month

Calendar mycal = new GregorianCalendar(iYear, iMonth, iDay);

// Get the number of days in that month

int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28

测试:在闰年中尝试一个月:

mycal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);

daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);      // 29

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值