http://blog.csdn.net/liwenfeng1022/article/details/6534176
//字符串日期加1天
String sgrq = "20140101";
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
try {
Date d = formatter.parse(sgrq);
String qxrq = formatter.format(d.getTime() + (1 * 24 * 60 * 60 * 1000));
} catch (ParseException e) {
e.printStackTrace();
}
1.用java.util.Calender来实现
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期
calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1
System.out.println(calendar.get(Calendar.DATE));//加1之后的日期Top
===============================================================================
2.用java.text.SimpleDateFormat和java.util.Date来实现
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("今天的日期:"+df.format(d));
System.out.println("两天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
===============================================================================
GregorianCalendar gc=new GregorianCalendar();
gc.setTime(new Date);
gc.add(field,value);
value为正则往后,为负则往前
field取1加1年,取2加半年,取3加一季度,取4加一周
取5加一天....
===============================================================================
/*
*java中对日期的加减操作
*gc.add(1,-1)表示年份减一.
*gc.add(2,-1)表示月份减一.
*gc.add(3.-1)表示周减一.
*gc.add(5,-1)表示天减一.
*以此类推应该可以精确的毫秒吧.没有再试.大家可以试试.
*GregorianCalendar类的add(int field,int amount)方法表示年月日加减.
*field参数表示年,月.日等.
*amount参数表示要加减的数量.
*
* UseDate.java 测试如下:
*/
package temp.util;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
public class UseDate {
Date d=new Date();
GregorianCalendar gc =new GregorianCalendar();
SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd");
public String getYears()
{
gc.setTime(d);
gc.add(1,+1);
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));
return sf.format(gc.getTime());
}
public String getHalfYear()
{
gc.setTime(d);
gc.add(2,+6);
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));
return sf.format(gc.getTime());
}
public String getQuarters()
{
gc.setTime(d);
gc.add(2,+3);
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));
return sf.format(gc.getTime());
}
public String getLocalDate()
{
return sf.format(d);
}
public static void main(String[] args)
{
UseDate ud= new UseDate();
System.out.println(ud.getLocalDate());
System.out.println(ud.getYears());
System.out.println(ud.getHalfYear());
System.out.println(ud.getQuarters());
}
}
//当月第一天和最后一天
SimpleDateFormat sf =new SimpleDateFormat("yyyyMMdd");
Calendar a=Calendar.getInstance();
int MaxDate=a.get(Calendar.DATE);
System.out.println("该月最大天数:"+MaxDate+","+sf.format(a.getTime()));
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
System.out.println("当月第一天:"+sf.format(a.getTime()));
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
System.out.println("当月最后一天:"+sf.format(a.getTime()));
4 ,
GregorianCalendar gc=new GregorianCalendar();
try {
gc.setTime( new SimpleDateFormat("yyyyMM").parse("200901"));
gc.add(2, -Integer.parseInt("7"));
} catch (ParseException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyyMM").format(gc.getTime()));
运行结果:200806