java当前日期的前n天
package com.stu.staservice.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 日期操作工具类 * * @author qy * @since 1.0 */ public class DateUtil { private static final String dateFormat = "yyyy-MM-dd"; /** * 格式化日期 * * @param date * @return */ public static String formatDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); return sdf.format(date); } /** * 在日期date上增加amount天 。 * * @param date 处理的日期,非null * @param amount 要加的天数,可能为负数 */ public static Date addDays(Date date, int amount) { Calendar now =Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE,now.get(Calendar.DATE)+amount); return now.getTime(); } public static void main(String[] args) { System.out.println(DateUtil.formatDate(new Date())); System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), -1))); System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), 11))); } }
运行结果
2021-08-15 2021-08-14 2021-08-26