栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java 特定年份的特定月份的天数?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 特定年份的特定月份的天数?

Java 8及更高版本
@沃伦·诺科斯(Warren M. 如果你尝试使用Java 8的新Date and Time API,则可以使用java.time.YearMonthclass。请参阅Oracle教程。

// Get the number of days in that monthYearMonth yearMonthObject = YearMonth.of(1999, 2);int daysInMonth = yearMonthObject.lengthOfMonth(); //28  

测试:尝试a年的一个月:

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 monthCalendar mycal = new GregorianCalendar(iYear, iMonth, iDay);// Get the number of days in that monthint daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28

测试:尝试a年的一个月:

mycal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);      // 29


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号