/** * 当前日期加上指定天数后 * @param num 为增加的天数 * @return */ public static String plusDay(int num){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar ca = Calendar.getInstance(); ca.add(Calendar.DATE, num); return format.format(ca.getTime()); }
/** * 给定日期加上指定天数后 * @param date * @param day * @return */ public static String addDate(Date date,long day) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long time = date.getTime(); day = day*24*60*60*1000; time+=day; return format.format(new Date(time)); }
@Test public void start(){ Boolean flag = true; Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { d = format.parse("2020-02-20 10:100syut:1"); } catch (ParseException e) { flag = false; System.out.println("转换错误!"); } if(flag)System.out.println(Num_test.addDate(d,5)); }