/**
input: 20190801
output: 20190701
*/
public static String getLastMonthStr(String currDate) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyyMMdd").parse(currDate);
} catch (ParseException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new RuntimeException(e);
}
c.setTime(date);
c.add(Calendar.MONTH, -1); //上个月
String lastMonthStr = new SimpleDateFormat("yyyyMMdd").format(c.getTime());
return lastMonthStr;
}
/**
input: 20201113
output: 20201213
*/
public static String getNextMonthStr(String currDate) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyyMMdd").parse(currDate);
} catch (ParseException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new RuntimeException(e);
}
c.setTime(date);
c.add(Calendar.MONTH, +1); //下个月
String lastMonthStr = new SimpleDateFormat("yyyyMMdd").format(c.getTime());
return lastMonthStr;
}
获取前后一个月的日期
最新推荐文章于 2024-05-07 14:23:03 发布