✅ 1. lengthOfMonth()
—— 获取当月的天数
🔹 功能
返回 LocalDate
对象所表示的 日期所在月份的总天数(28、29、30 或 31)。
🔹 方法签名
public int lengthOfMonth()
🔹 返回值
28
:平年2月29
:闰年2月30
:小月(4、6、9、11月)31
:大月(1、3、5、7、8、10、12月)
🔹 代码示例
import java.time.LocalDate;
// 2025年8月15日(8月有31天)
LocalDate date1 = LocalDate.of(2025, 8, 15);
System.out.println(date1 + " 所在月有 " + date1.lengthOfMonth() + " 天"); // 31
// 2024年2月10日(2024是闰年,2月有29天)
LocalDate date2 = LocalDate.of(2024, 2, 10);
System.out.println(date2 + " 所在月有 " + date2.lengthOfMonth() + " 天"); // 29
// 2025年2月10日(2025不是闰年,2月有28天)
LocalDate date3 = LocalDate.of(2025, 2, 10);
System.out.println(date3 + " 所在月有 " + date3.lengthOfMonth() + " 天"); // 28
输出:
2025-08-15 所在月有 31 天 2024-02-10 所在月有 29 天 2025-02-10 所在月有 28 天
✅ 2. lengthOfYear()
—— 获取当年的天数
🔹 功能
返回 LocalDate
对象所表示的 日期所在年份的总天数。
🔹 方法签名
public int lengthOfYear()
🔹 返回值
365
:平年366
:闰年
🔹 代码示例
LocalDate date1 = LocalDate.of(2024, 1, 1); // 2024 是闰年
System.out.println(date1.getYear() + " 年有 " + date1.lengthOfYear() + " 天"); // 366
LocalDate date2 = LocalDate.of(2025, 1, 1); // 2025 是平年
System.out.println(date2.getYear() + " 年有 " + date2.lengthOfYear() + " 天"); // 365
输出:
2024 年有 366 天 2025 年有 365 天
✅ 3. isLeapYear()
—— 判断是否为闰年
🔹 功能
判断 LocalDate
对象所表示的 日期所在年份是否为闰年。
🔹 方法签名
public boolean isLeapYear()
🔹 返回值
true
:是闰年false
:不是闰年
🔹 闰年规则(ISO-8601)
- 能被 4 整除,但不能被 100 整除;
- 或者能被 400 整除。
🔹 代码示例
LocalDate date1 = LocalDate.of(2024, 1, 1);
System.out.println(date1.getYear() + " 是闰年吗?" + date1.isLeapYear()); // true
LocalDate date2 = LocalDate.of(2025, 1, 1);
System.out.println(date2.getYear() + " 是闰年吗?" + date2.isLeapYear()); // false
LocalDate date3 = LocalDate.of(2000, 1, 1); // 能被400整除 → 闰年
System.out.println(date3.getYear() + " 是闰年吗?" + date3.isLeapYear()); // true
LocalDate date4 = LocalDate.of(1900, 1, 1); // 能被100整除但不能被400整除 → 平年
System.out.println(date4.getYear() + " 是闰年吗?" + date4.isLeapYear()); // false
输出:
2024 是闰年吗?true 2025 是闰年吗?false 2000 是闰年吗?true 1900 是闰年吗?false
🔗 三者关系与实用技巧
这三个方法通常结合使用,用于处理与 月份长度、年份长度、闰年逻辑 相关的业务。
✅ 实用场景示例
场景:判断今天是否是当月最后一天
LocalDate today = LocalDate.now();
boolean isLastDayOfMonth = today.getDayOfMonth() == today.lengthOfMonth();
if (isLastDayOfMonth) {
System.out.println("今天是 " + today.getMonth() + " 月的最后一天!");
}
场景:计算今年还剩多少天
LocalDate today = LocalDate.now();
LocalDate endOfYear = today.withDayOfYear(today.lengthOfYear());
long daysLeft = java.time.temporal.ChronoUnit.DAYS.between(today, endOfYear);
System.out.println("今年还剩 " + daysLeft + " 天");
场景:动态生成每月报表(考虑2月天数)
LocalDate reportDate = LocalDate.of(2024, 2, 1);
int daysInMonth = reportDate.lengthOfMonth();
System.out.println("报表周期:1-" + daysInMonth + " 日"); // 1-29 日(闰年)
⚠️ 注意事项
问题 | 说明 |
---|---|
方法基于对象的年月 | lengthOfMonth() 返回的是该 LocalDate 所在月份的天数,与具体哪一天无关。LocalDate.of(2024,2,1).lengthOfMonth() 和 LocalDate.of(2024,2,29).lengthOfMonth() 都返回 29 。 |
不可变性 | 这些方法不修改原对象,只返回信息。 |
时区无关 | LocalDate 不含时区,因此这些计算基于日历系统本身。 |
性能 | 这些方法计算简单,性能极高,可频繁调用。 |
✅ 总结
方法 | 返回类型 | 功能 | 示例 |
---|---|---|---|
lengthOfMonth() |
int |
返回当月天数 | 31 |
lengthOfYear() |
int |
返回当年天数 | 365 或 366 |
isLeapYear() |
boolean |
判断是否为闰年 | true / false |