/** * 向前推算时间,并转换成指定格式输出 * @param date 参照日期 * @param days 变动的天数 * @param daytype 返回的样式 eg: yyyy-MM-dd HH:mm:ss * @return */ public static String getBeforeDate(Date date, int days, String daytype) { SimpleDateFormat df = new SimpleDateFormat(daytype); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days); return df.format(calendar.getTime()); }
/** * 向后推算时间,并转换成指定格式输出 * @param date 参照日期 * @param days 变动的天数 * @param daytype 返回的样式 eg: yyyy-MM-dd HH:mm:ss * @return */public static String getAfterDate(Date date, int days, String daytype) { SimpleDateFormat df = new SimpleDateFormat(daytype); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days); return df.format(calendar.getTime());}