1. 程式人生 > >Java的日期修改(如增加一天)

Java的日期修改(如增加一天)

最近用到了日期的新增與修改,下面一起學習:

import java.util.Date ;
     Date date=new   Date ();//取時間 
     Calendar calendar = new GregorianCalendar(); 
     calendar.setTime(date); 
     calendar.add(Calendar.DATE,1);//把日期往後增加一天.正數往後推,負數往前推 
     date=calendar.getTime();   //這個時間就是日期往後推一天的結果 

Calendar.DATECalendar.DAY_OF_MONTH

是一個意思,表示一個月中的一天。還有其他的表示分鐘,小時等的欄位

add(int field, int amount) 
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar’s rules.