Dateadd(String str,int count)//str为形如yyyy-mm-dd的字符串格式的日期,count为给月份加的整数值。代码如下:
package java_Thread;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class myDate {
public static void main(String[] args) throws ParseException{
Dateadd("2008-8-31",6);
}
public static void Dateadd(String str,int count) throws ParseException{ //str 类型为 yyyy-mm-dd
Date df = DateFormat.getDateInstance(2).parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(df);
show(calendar.getTime());
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + count);
show(calendar.getTime());
}
public static void show(Date a){
DateFormat df2 = DateFormat.getDateInstance(2);
System.out.println(df2.format(a));
}
}
结果如下:
2008-8-31
2009-3-3