java日期减去n天

Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			System.out.println(cal.getTime());
			System.out.println(cal.get(Calendar.DAY_OF_MONTH));// 今天的日期
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 5);// 让日期减去5
			Date endDate = cal.getTime();
			System.out.println(cal.getTime());
  1. 结果
    在这里插入图片描述

你可能感兴趣的:(#,JAVA基础)