/**
* 返回指定天前的日期 yyyyMMdd
*
* @param days
* days天前的日期
*
* @return
*/
private static String dateBeforeNow(int days) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
cal.setTime(new Date());
cal.add(Calendar.DATE, -days);
String time = format.format(cal.getTime());
return time;
}
public static void main(String[] args) {
String dateBeforeNow = dateBeforeNow(30);//前30天前的日期
System.out.println(dateBeforeNow);
}
days天前的时间
/**
* 获取days天前的时间戳
*
* @param days
* 天数
* @return Integer
*/
private static Integer getTimestampBeforeNow(int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, -days);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long time = cal.getTime().getTime() / 1000;
return (int) time;
}
days天前的时间戳