转自:http://blog.sina.com.cn/s/blog_a2614218010188lb.html

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateTestUtil {
 
    
    public static void main(String[] args) throws Exception {
        
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String str="20110823";
        Date dt=sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR,-1);//日期减1年
        rightNow.add(Calendar.MONTH,3);//日期加3个月
        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
        Date dt1=rightNow.getTime();
        String reStr = sdf.format(dt1);
        System.out.println(reStr);
 
    }
 
}
 
注:在Calendar对象的add方法中,第二个参数为正数表示“加”,负数表示“减”
 
 
获取系统当前时间(String类型)
public static String getStringDateShort() {
     Date currentTime = new Date();
     SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
     String dateString = formatter.format(currentTime);
     return dateString;
    } 
 
posted on 2018-04-19 10:18  Sharpest  阅读(1622)  评论(0编辑  收藏  举报