Java Year 类
最后修改时间:2025 年 4 月 16 日
java.time.Year
类表示 ISO-8601 日历系统中的年份。 它是 Java 8 日期和时间 API 的一部分,提供无需日期或时间信息的年份操作方法。 Year 类是不可变的并且线程安全的。
当您只需要处理日期的年份部分时,Year
类就很有用。 它支持从 -999,999,999 到 +999,999,999 的年份。 该类提供了检查闰年、比较年份和执行算术运算的方法。
Year 类概述
Year
类提供了用于创建实例的静态工厂方法以及用于查询和操作年份的各种方法。 关键操作包括检查有效性、比较年份以及转换为其他时间类型。
public final class Year implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable { public static Year now(); public static Year of(int isoYear); public static Year parse(CharSequence text); public int getValue(); public boolean isLeap(); public boolean isValidMonthDay(MonthDay monthDay); public boolean isAfter(Year otherYear); public boolean isBefore(Year otherYear); public Year plusYears(long yearsToAdd); public Year minusYears(long yearsToSubtract); }
上面的代码展示了 Year
类提供的关键方法。 这些方法允许创建、比较和操作年份值。 该类提供了遵循 ISO-8601 规则的验证和闰年计算。
创建 Year 对象
可以通过多种方式创建 Year 对象。 最常用的方法是使用 now
获取当前年份,使用 of
获取特定年份。 也支持从字符串解析。
package com.zetcode; import java.time.Year; public class Main { public static void main(String[] args) { // Current year Year currentYear = Year.now(); System.out.println("Current year: " + currentYear); // Specific year Year year2025 = Year.of(2025); System.out.println("Year 2025: " + year2025); // Parse from string Year parsedYear = Year.parse("2030"); System.out.println("Parsed year: " + parsedYear); // From system clock with specific zone Year yearInTokyo = Year.now(ZoneId.of("Asia/Tokyo")); System.out.println("Current year in Tokyo: " + yearInTokyo); } }
此示例演示了创建 Year 对象的不同方法。 输出显示了 YYYY 格式的年份值。 now
方法可以使用系统默认时区或指定时区。
获取年份信息
Year 对象提供获取其值和检查属性(例如是否为闰年)的方法。 这些方法对于应用程序中的基于年份的逻辑至关重要。
package com.zetcode; import java.time.Year; public class Main { public static void main(String[] args) { Year year = Year.of(2024); // Get year value int yearValue = year.getValue(); System.out.println("Year value: " + yearValue); // Check if leap year boolean isLeap = year.isLeap(); System.out.println("Is leap year: " + isLeap); // Check validity of month-day boolean isValid = year.isValidMonthDay(MonthDay.of(2, 29)); System.out.println("Is Feb 29 valid: " + isValid); // Length of year int length = year.length(); System.out.println("Days in year: " + length); } }
此示例演示了如何从 Year 对象获取信息。 闰年检查遵循 ISO-8601 规则。 isValidMonthDay
方法对于验证日期而无需创建完整的日期对象特别有用。
比较年份
可以比较年份以确定时间顺序。 该类提供了 isBefore
, isAfter
和 compareTo
方法。 这些比较对于排序和条件逻辑很有用。
package com.zetcode; import java.time.Year; public class Main { public static void main(String[] args) { Year current = Year.now(); Year nextYear = current.plusYears(1); Year lastYear = current.minusYears(1); System.out.println("Current is before next: " + current.isBefore(nextYear)); System.out.println("Current is after last: " + current.isAfter(lastYear)); System.out.println("Comparison result: " + current.compareTo(nextYear)); // Equality check Year sameYear = Year.of(current.getValue()); System.out.println("Current equals same year: " + current.equals(sameYear)); } }
此示例演示了比较 Year 对象的各种方法。 比较方法仅考虑年份值。 请注意,相等需要完全相同的年份值。
加减年份
Year 类通过 plusYears
和 minusYears
方法支持算术运算。 这些运算对于计算未来或过去的年份很有用。 该类会自动处理年份回滚。
package com.zetcode; import java.time.Year; public class Main { public static void main(String[] args) { Year year = Year.of(2025); // Add years Year future = year.plusYears(5); System.out.println("Five years later: " + future); // Subtract years Year past = year.minusYears(10); System.out.println("Ten years ago: " + past); // Leap year calculation Year leapYear = year.plusYears(4); System.out.println("Next leap year: " + leapYear + " (" + leapYear.isLeap() + ")"); // Large year changes Year distantFuture = year.plusYears(1000); System.out.println("Year 3025: " + distantFuture); } }
此示例演示了如何使用 Year 对象执行算术运算。 运算可以跨越很大的年份范围,并自动保持闰年属性。 结果始终是有效的 Year 对象。
在时间类型之间转换
Year 可以转换为其他时间类型(例如 LocalDate),也可以从其他时间类型转换。 当使用更完整的日期表示时,这些转换至关重要。
package com.zetcode; import java.time.Year; import java.time.LocalDate; import java.time.Month; public class Main { public static void main(String[] args) { Year year = Year.of(2025); // Convert to LocalDate (first day of year) LocalDate date = year.atDay(1); System.out.println("First day of year: " + date); // Convert with specific month-day LocalDate christmas = year.atMonthDay(MonthDay.of(Month.DECEMBER, 25)); System.out.println("Christmas: " + christmas); // Convert from LocalDate Year fromDate = Year.from(LocalDate.of(2030, 6, 15)); System.out.println("Year from date: " + fromDate); // Convert to YearMonth YearMonth june = year.atMonth(Month.JUNE); System.out.println("June of year: " + june); } }
此示例演示了 Year 和其他时间类型之间的转换。 atDay
和 atMonthDay
方法对于创建完整的日期特别有用。 所有转换都保持时间精度。
验证日期
Year 类提供了在不创建完整日期对象的情况下验证月-日组合的方法。 这对于检查特定日期是否存在于给定年份中很有用。
package com.zetcode; import java.time.Year; import java.time.Month; import java.time.MonthDay; public class Main { public static void main(String[] args) { Year leapYear = Year.of(2024); Year nonLeapYear = Year.of(2025); // Check February 29 MonthDay feb29 = MonthDay.of(Month.FEBRUARY, 29); System.out.println("Feb 29 in 2024: " + leapYear.isValidMonthDay(feb29)); System.out.println("Feb 29 in 2025: " + nonLeapYear.isValidMonthDay(feb29)); // Check April 31 MonthDay apr31 = MonthDay.of(Month.APRIL, 31); System.out.println("Apr 31 in any year: " + Year.of(2000).isValidMonthDay(apr31)); // Check December 32 MonthDay dec32 = MonthDay.of(Month.DECEMBER, 32); System.out.println("Dec 32 in any year: " + Year.of(2000).isValidMonthDay(dec32)); } }
此示例展示了如何根据特定年份验证月-日组合。 isValidMonthDay
方法检查月份长度以及特殊情况,例如闰年的 2 月 29 日。
来源
在本文中,我们介绍了 Java Year 类的基本方法和特性。 了解这些概念对于在现代 Java 应用程序中使用基于年份的数据至关重要。
作者
列出所有Java教程。