做整年的日期的数据的按月归档及数据结构的用到当前月份的最大日子,so 代码如下
LocalDate date = LocalDate.parse(calDay, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
int maxDay=date.getMonth().maxLength();
这串JAVA8日期处理用完觉得很润是不是,But在接下来调试中发现在2022-02月的日子里这样获取返回值竟然是29,29天,有点不对劲。查了windows的日历表,2022-02月份的日子数一共是28天才对。
查查我生成的数据是没问题的,因此直接debug喽。
真的给我搞个29,太错误了吧,跟进api看源码
/**
* Gets the maximum length of this month in days.
* <p>
* February has a maximum length of 29 days.
* April, June, September and November have 30 days.
* All other months have 31 days.
*
* @return the maximum length of this month in days, from 29 to 31
*/
public int maxLength() {
switch (this) {
case FEBRUARY:
return 29;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
}
怎么看也不对劲,凑巧上边还有个方法
/**
* Gets the minimum length of this month in days.
* <p>
* February has a minimum length of 28 days.
* April, June, September and November have 30 days.
* All other months have 31 days.
*
* @return the minimum length of this month in days, from 28 to 31
*/
public int minLength() {
switch (this) {
case FEBRUARY:
return 28;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
}
感觉有点像Calenda类当时的minLength()其实是实际的日子数吧,但我还是不确定直接用这api闰年怎么搞,不能我还判断年份吧,超麻烦了,直接判断日期算了,代码如下:
date.with(TemporalAdjusters.lastDayOfMonth())
直接判断日期是否是本月最后一天即可。