java 用Java查找一个月中的天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2545110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the number of days in a month in Java
提问by chetan
How do you find the number of days in a month in Java?
你如何在Java中找到一个月中的天数?
回答by Mark Byers
Set the year and month on a Calendar object and then use getActualMaximumto return the last day:
在 Calendar 对象上设置年和月,然后使用getActualMaximum返回最后一天:
calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
回答by Basil Bourque
java.time.Month
java.time.Month
Using the java.time classes, the java.time.Monthenum in particular.
使用 java.time 类,java.time.Month特别是枚举。
int days = Month.FEBRUARY.minLength(); // 28
int days = Month.FEBRUARY.maxLength(); // 29
int days = Month.FEBRUARY.length( boolean_consider_leap_year ); // TRUE → 29, FALSE → 28.
You can get the Monthobject for a month number, 1-12 meaning January-December.
您可以获取Month月份编号的对象,1-12 表示一月至十二月。
int monthNumber = Month.FEBRUARY.getValue();
About java.time
关于 java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧日期时间类,例如java.util.Date, .Calendar, & java.text.SimpleDateFormat。
The Joda-Timeproject, now in maintenance mode, advises migration to java.time.
现在处于维护模式的Joda-Time项目建议迁移到 java.time。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backportand further adapted to Androidin ThreeTenABP(see How to use…).
大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植,并进一步用于安卓在ThreeTenABP(见如何使用......)。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,如Interval,YearWeek,YearQuarter,等。
回答by ?yvind Mo
Since Java 8, a simple way would be:
从 Java 8 开始,一个简单的方法是:
int daysInCurrentMonth = java.time.LocalDate.now().lengthOfMonth();

