1、不要在程序中写死一年为 365 天,避免在公历闰年时出现日期转换错误或程序逻辑错误。
2020年有366天
// 获取今年的天数
int dayOfYear = LocalDate.now().lengthOfYear();
2、获取指定年份的天数
2019年有365天
// 获取指定年份的天数,例如:2019
int daysOf2019 = LocalDate.of(2019, 1, 1).lengthOfYear();
1、不要在程序中写死一年为 365 天,避免在公历闰年时出现日期转换错误或程序逻辑错误。
2020年有366天
// 获取今年的天数
int dayOfYear = LocalDate.now().lengthOfYear();
2、获取指定年份的天数
2019年有365天
// 获取指定年份的天数,例如:2019
int daysOf2019 = LocalDate.of(2019, 1, 1).lengthOfYear();