時間型データの演算
Calendar add(int field, int amount) 。 :
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TestCalendar {
publicstatic void main(String[] args){
Calendar c = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
//
Date date=format.parse("2011-08-05 14:49:45");
c.setTime(format.parse("2011-08-05 14:49:45"));
System.out.println(" :"+format.format(c.getTime()));
c.add(Calendar.MINUTE,20);
System.out.println(" :"+format.format(c.getTime()));
}catch (ParseException e) {
e.printStackTrace();
}
}
}
:
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(DateUtil.getDatePattern());
createDate = df.format(today);
, , 。
public static int getDay(Date date) {
if (date == null) {
return 0;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_YEAR);
}