首页 > 代码库 > Java 年月日 日期加减

Java 年月日 日期加减

 

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();
    }

 

Java 年月日 日期加减