获取某月前日期 ,并比较当前月与某月是否相差 X月内
public class test3 {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
//获取
Calendar nowTime = Calendar.getInstance();
nowTime.add(Calendar.MONTH, -2); //当前日期-3个月
Date time = nowTime.getTime();
String formatStr = format.format(time);
System.out.println("格式化后的时间:"+formatStr);
}
public static String getLastMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 设置为当前时间
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
date = calendar.getTime();
String lastMonth = format.format(date);
return lastMonth;
}
}
}