我们使用三方组件比较熟悉的hutool:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.25</version>
</dependency>
那我们就来看看如何获取本月/上月:
// 本月
DateUtil.beginOfMonth(new Date()
DateUtil.endOfMonth(new Date())
// 上月
DateUtil.beginOfMonth(DateUtil.offsetMonth(new Date(), -1))
DateUtil.endOfMonth(DateUtil.offsetMonth(new Date(), -1))
前几天:
String days = DateUtil.offsetDay(new Date(), -3).toDateStr();