public class DateControl {
public static String getBeforeAfterDate(String datestr, int day) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
java.sql.Date olddate = null;
try {
df.setLenient(false);
olddate = new java.sql.Date(df.parse(datestr).getTime());
} catch (ParseException e) {
throw new RuntimeException("日期转换错误");
}
Calendar cal = new GregorianCalendar();
cal.setTime(olddate);
int Year = cal.get(Calendar.YEAR);
int Month = cal.get(Calendar.MONTH);
int Day = cal.get(Calendar.DAY_OF_MONTH);
int NewDay = Day + day;
cal.set(Calendar.YEAR, Year);
cal.set(Calendar.MONTH, Month);
cal.set(Calendar.DAY_OF_MONTH, NewDay);
String res=(String)new java.sql.Date(cal.getTimeInMillis()).toString();
String operdate=df.format(new java.sql.Date(cal.getTimeInMillis()));
return operdate;
}
public static void main(String[] args) throws Exception {
System.out.println(DateControl.getBeforeAfterDate("20090815",31));
}
}