java日期加减运算-转

原文链接 http://www.blogjava.net/willpower88/archive/2006/12/07/86012.html

JAVA 日期加减运算
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() - (long)2 * 24 * 60 * 60 * 1000)));  
          System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + (long) 3 * 24 * 60 * 60 * 1000)));

你可能感兴趣的:(java,html,.net)