求java Date类型字段加一天代码
String startdate=UIDBComboBox1.getValue();
startdate是日期字断,求java Date类型字段加一天代码 展开
Calendar calendar =
new GregorianCalendar();
calendar.setTime((Data)startdate); //你自己的数据进行类型转换
calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
date=calendar.getTime();
date就是增加一天以后的数据,如果需要的话,还可以对时分秒进行增减
String startdate=UIDBComboBox1.getValue();
Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(startdate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,1);
date =cal.getTime();
startdate = (new SimpleDateFormat("yyyy-MM-dd")).format(date);
举例:
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class $ {
public static void main(String[] args) {
Format f = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
System.out.println("今天是:" + f.format(today));
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
Date tomorrow = c.getTime();
System.out.println("明天是:" + f.format(tomorrow));
}
}
结果:
今天是:2013-10-09
明天是:2013-10-10
用Calendar方便
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class $ {
public static void main(String[] args) {
Format f = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
System.out.println("今天是:" + f.format(today));
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
Date tomorrow = c.getTime();
System.out.println("明天是:" + f.format(tomorrow));
}
}
结果:
今天是:2013-10-09
明天是:2013-10-10
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
System.out.println((new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime()));
Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(startdate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,1);
date =cal.getTime();
startdate = (new SimpleDateFormat("yyyy-MM-dd")).format(date);//加一天后结果