当前日期加20天

来源:互联网 发布:儿童节礼物 知乎 编辑:程序博客网 时间:2024/05/20 19:14
package com.itheima.poxxxcc;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo1 {
    public static void main(String[] args) throws Exception {
        String time = getAfterTime("2016-08-1",20);
        System.out.println(time);
    }
    public static String getAfterTime(String date,int amount) throws Exception{
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        将字符串转成日期
        Date d = sf.parse(date);
//        获得calendar对象
        Calendar calendar = Calendar.getInstance();
//        设置当前时间
        calendar.setTime(d);
//        在当前时间下加若干天
        calendar.add(calendar.DAY_OF_MONTH, amount);
        Date currTime = calendar.getTime();
//        将日期转换成字符串
        String lastTime = sf.format(currTime);
        return lastTime;
        
    }
}
0 0