Java實現增加天數得到新的日期
import java.util.Date;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class ceshi2 {
public static Date addDate(Date d, long day) throws ParseException {
long time = d.getTime();
day = day * 24 * 60 * 60 * 1000;
time += day;
return new Date(time);
}
public static void main(String[] args) {
/*
* java.util.Date dt=new java.util.Date(); System.out.println(dt);
* //輸出結果是:Wed Aug 10 11:29:11 CST 2005
*
* SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
* System.out.print(sdf.format(dt) + 12);
*/
Date d = new Date();
Date newDate = addDate(d, 12);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormat.format(d));
System.out.println(dateFormat.format(newDate));
}
}