方法定义
getYear()
public int getYear()
getMonth()
public Month getMonth()
- 返回值:返回一个
Month
枚举对象,表示月份(如 JANUARY
, FEBRUARY
等)
- 类型:
java.time.Month
(枚举)
getMonthValue()
public int getMonthValue()
- 返回值:返回月份的数值(1-12),1 表示一月,12 表示十二月
- 类型:
int
功能说明
这三个方法用于从 LocalDate
对象中提取年份和月份信息,是处理日期解析、格式化、条件判断等场景的常用方法。
方法 |
返回类型 |
返回示例 |
用途 |
getYear() |
int |
2025 |
获取年份 |
getMonth() |
Month 枚举 |
Month.AUGUST |
获取月份枚举(适合 switch、语义清晰) |
getMonthValue() |
int |
8 |
获取月份数值(适合计算、比较) |
示例代码
基本用法
import java.time.LocalDate;
import java.time.Month;
public class LocalDateGetExample {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2025, 8, 15); // 2025年8月15日
// 获取年份
int year = date.getYear();
System.out.println("年份: " + year); // 输出: 2025
// 获取月份(枚举)
Month month = date.getMonth();
System.out.println("月份(枚举): " + month); // 输出: AUGUST
System.out.println("月份名称: " + month.name()); // AUGUST
System.out.println("本地化名称: " + month.getDisplayName(TextStyle.FULL, Locale.CHINESE)); // 八月
// 获取月份数值
int monthValue = date.getMonthValue();
System.out.println("月份(数值): " + monthValue); // 输出: 8
}
}
实际应用场景
场景 1:判断是否为当前年份
LocalDate today = LocalDate.now();
LocalDate birthDate = LocalDate.of(2000, 5, 20);
if (birthDate.getYear() == today.getYear()) {
System.out.println("生日在今年");
} else {
System.out.println("生日不在今年");
}
场景 2:根据月份执行不同逻辑(使用枚举)
Month currentMonth = LocalDate.now().getMonth();
switch (currentMonth) {
case DECEMBER:
case JANUARY:
case FEBRUARY:
System.out.println("冬季");
break;
case MARCH:
case APRIL:
case MAY:
System.out.println("春季");
break;
// 其他季节...
default:
System.out.println("夏季");
}
场景 3:月份比较与计算
LocalDate date1 = LocalDate.of(2025, 3, 10);
LocalDate date2 = LocalDate.of(2025, 8, 15);
if (date1.getMonthValue() < date2.getMonthValue()) {
System.out.println("date1 的月份更早");
}
// 计算相差几个月(简化示例)
int diff = date2.getMonthValue() - date1.getMonthValue();
System.out.println("相差 " + diff + " 个月");
使用技巧
技巧 1:结合 Month
枚举的丰富方法
Month month = LocalDate.now().getMonth();
// 获取下一个月
Month next = month.plus(1); // SEPTEMBER
// 获取该月的天数(需结合年份)
int length = month.length(Year.isLeap(2025)); // 是否闰年影响2月
// 获取英文全称、缩写等
System.out.println(month.name()); // AUGUST
System.out.println(month.toString()); // AUG
技巧 2:格式化输出月份
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月");
System.out.println(date.format(formatter)); // 2025年8月
技巧 3:用于分组或索引
// 例如:按年月分组数据
String yearMonthKey = date.getYear() + "-" + String.format("%02d", date.getMonthValue());
// 结果: 2025-08
常见错误
错误 |
说明 |
正确做法 |
混淆 getMonth() 与 getMonthValue() |
以为 getMonth() 返回 int |
getMonth() 返回 Month 枚举 |
月份从 0 开始? |
误以为 getMonthValue() 从 0 开始 |
它从 1 开始(1=一月) |
直接打印 Month 枚举 |
输出 AUGUST 而非 8 |
需要数值时用 getMonthValue() |
忘记处理 null |
调用 null 对象的方法 |
提前判空 |
// 错误示例
LocalDate date = null;
int m = date.getMonthValue(); // 抛出 NullPointerException
注意事项
- ⚠️
getMonthValue()
返回 1-12:不是 0-11(与旧的 Calendar
不同)。
- ⚠️
getMonth()
返回枚举:不能直接用于数学计算,需调用 .getValue()
。
- ⚠️ 不可变性:这些方法不修改原
LocalDate
,返回的是提取的值。
- ⚠️ 时区无关:
LocalDate
不含时区,这些方法也不受时区影响。
- ⚠️ 性能:这些方法是轻量级的,可频繁调用。
最佳实践与性能优化
✅ 最佳实践
实践 |
说明 |
✅ 使用 getMonth() 进行语义判断 |
如 if (month == Month.AUGUST) |
✅ 使用 getMonthValue() 进行数值比较或计算 |
如 if (monthValue > 6) |
✅ 年份比较直接用 int |
getYear() 返回 int ,适合比较 |
✅ 避免字符串拼接判断 |
不要用 toString() 解析年月 |
⚙️ 性能优化
总结
方法 |
返回类型 |
范围/示例 |
推荐用途 |
getYear() |
int |
1970 , 2025 , 9999 |
年份比较、提取、计算 |
getMonth() |
Month 枚举 |
JANUARY , AUGUST |
switch 分支、语义判断、本地化输出 |
getMonthValue() |
int |
1 (一月)到 12 (十二月) |
数值比较、数学计算、数据库索引 |