发现 LocalDate 接收不了年月,只能用 String 接收。
后来偶然发现 java.time 里面有个类 YearMonth
定义请求实体
@DateTimeFormat(pattern = "yyyy-MM")
@JsonFormat(pattern = "yyyy-MM")
private YearMonth yearMonth;
数据库不能保存年月 用 LocalDate 接收
private LocalDate planMonth;
LocalDate、YearMonth 互转
/**
* 转换YearMonth为LocalDate
*/
public static LocalDate parseDateYearMonth(YearMonth yearMonth){
return LocalDate.of(yearMonth.getYear(),yearMonth.getMonthValue(),1);
}
/**
* 转换LocalDate为YearMonth
*/
public static YearMonth parseYearMonth(LocalDate localDate){
return YearMonth.of(localDate.getYear(),localDate.getMonthValue());
}
5923

被折叠的 条评论
为什么被折叠?



