在Java中寻找闰年 Baeldung
source link: https://www.jdon.com/51568.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在Java中寻找闰年 Baeldung
闰年是可以被4和400整除而没有余数的一年.
1. 使用Pre-Java-8 Calendar API
从Java 1.1开始, GregorianCalendar 类允许我们检查一年是否是闰年:
public boolean isLeapYear(int year);
如果给定的年份是闰年,则此方法返回true,而非闰年则 返回false。
2.使用Java 8+日期/时间API
Java8引入 java.time具有更好的日期和时间API。该包有一个静态方法来检查给定年份是否为闰年:
public static boolean isLeap(long year);
3. 使用Joda-Time API
Joda-Time API 是Java项目的日期和时间最常用的第三方库之一。从Java 8开始,这个库处于可维护状态。在Joda-Time中找不到闰年没有预先定义的API方法。但是,我们可以使用他们的LocalDate和Days类来检查闰年:
LocalDate localDate = new LocalDate(2020, 1, 31); |
以在GitHub上找到代码段
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK