相关链接:【Java-----日期类(三)之Calendar类详解】_calendar java_筱筱的博客-CSDN博客
/**
* 获取指定日期的下一个月
* @return
* @throws ParseException
*/
public static String nextMonth() throws ParseException {
Date startData = new SimpleDateFormat("yyyy-MM-dd").parse("2023-04-01");
LocalDateTime localDateTime = startData.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime()
.plusMonths(1);
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}
/**
* 获取指定日期的上一个月
* @return
* @throws ParseException
*/
public static String lastMonth() throws ParseException {
Date startData = new SimpleDateFormat("yyyy-MM-dd").parse("2023-04-01");
LocalDateTime localDateTime = startData.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime()
.plusMonths(-1);
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}