Java的java.time包提供日期,时间,实例和持续时间的API。它提供了各种类,例如Clock,LocalDate,LocalDateTime,LocalTime,MonthDay,Year,YearMonth等。与以前的替代方法相比,使用此软件包的类,您可以以更简单的方式获取与日期和时间相关的详细信息。
Java.time.LocalDate-此类表示ISO- 8601日历系统中不带时区的日期对象。now()
此类的方法从系统时钟获取当前日期。
该isLeapYear()
方法java.time.LocalDate 验证是否在当前对象的年份是根据ISO proleptic日历系统规则闰年,返回true,如果是这样,否则返回false。
随后的Java程序获取当前日期,并确定是否为a年。
import java.time.LocalDate; public class IsLeapYear { public static void main(String args[]) { //获取当前日期 LocalDate currentDate = LocalDate.now(); //验证leap年 boolean bool = currentDate.isLeapYear(); //在 if(bool){ System.out.println("Current year is a leap year "); }else{ System.out.println("Current year is not a leap year "); } } }
输出结果
Current year is not a leap year
以下示例接受用户的年份并显示是否为a年。
import java.time.LocalDate; import java.util.Scanner; public class IsLeapYear { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the year: "); int year = sc.nextInt(); //获取给定日期值jan1st的日期 LocalDate givenDate = LocalDate.of(year, 01, 01); //验证leap年 boolean bool = givenDate.isLeapYear(); //在 if(bool){ System.out.println("Given year is a leap year "); }else{ System.out.println("Given year is not a leap year "); } } }
输出结果
Enter the year: 2004 Given year is a leap year