Calendar提供了获取、设置各种日历字段的方法
构造方法:protected Calendar(); 由于修饰符为protected,无法直接创建对象,但可以使用静态方法
static Calendar getInstance();
import java.util.Calendar;
public class TestCalendar {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(calendar.YEAR);
int month = calendar.get(calendar.MONTH);
int day = calendar.get(calendar.DAY_OF_MONTH);
int hour = calendar.get(calendar.HOUR);
int minute = calendar.get(calendar.MINUTE);
int second = calendar.get(calendar.SECOND);
System.out.println(year + "年" + (month+1) + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒");
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH,1);
System.out.println(calendar2.getTime().toLocaleString());
calendar2.add(calendar2.HOUR,2);
System.out.println(calendar2.getTime().toLocaleString());
int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max);
System.out.println(min);
}
}
|