public static String DATE_YEAR="YEAR";//年 public static String DATE_MONTH="MONTH";//月 public static String DATE_DAY="DAY";//日
//7天 Date date_day = dateOperation(DATE_DAY,-7); //1个月 Date date_month = dateOperation(DATE_MONTH,-1); //1年 Date date_year = dateOperation(DATE_YEAR,-1);
private Date dateOperation(String dateOperationType,Integer amount) throws Exception { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String s = sdf.format(new Date()); Date today = sdf.parse(s); cal.setTime(today); if (DATE_DAY.equals(dateOperationType)){ cal.add(cal.DAY_OF_MONTH,amount); } if (DATE_MONTH.equals(dateOperationType)){ cal.add(cal.MONTH,amount); } if (DATE_YEAR.equals(dateOperationType)){ cal.add(cal.YEAR,amount); } // System.out.println(cal.getTime()); return cal.getTime(); }
时间: 2024-12-25 05:37:02