先上代码,-distanceDay代表要获取当前N天前的日期,
+distanceDay代表要获取当前N天后的日期
public static String getOldDate(int distanceDay) { SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd"); Date beginDate = new Date(); Calendar date = Calendar.getInstance(); date.setTime(beginDate); date.set(Calendar.DATE, date.get(Calendar.DATE) - distanceDay); Date endDate = null; try { endDate = dft.parse(dft.format(date.getTime())); } catch (ParseException e) { e.printStackTrace(); } return dft.format(endDate); }