- calendar类提供了获取和设置日历字段的方法
- 构造方法
- protected Calendar();由于修饰符是protected所以无法创建该对象
主要方法
- static Calendar getInstance():使用默认时区和区域获取日历
- void set(int year ,int mouth,int day):设置年月日
- int get(int filed):返回日历字段值,如年月日等
- void setTime:用设置的Date方法设置此日历的时间 Date-calendar
- void add():添加时间
- long getTimeInMillies():毫秒为单位返回该日历的时间值
package blogtest.oop2.calendar;
import java.util.Calendar;
public class Demo01 {
public static void main(String[] args) {
Calendar num1 = Calendar.getInstance();
System.out.println(num1.getTime().toLocaleString());
int year = num1.get(Calendar.YEAR);
int mouth = num1.get(Calendar.MONTH);
int day = num1.get(Calendar.DAY_OF_MONTH);
int hour = num1.get(Calendar.HOUR_OF_DAY);
int minute = num1.get(Calendar.MINUTE);
int second = num1.get(Calendar.SECOND);
System.out.println(year+"年"+(mouth+1)+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒");
Calendar num2 = Calendar.getInstance();
num2.set(Calendar.HOUR_OF_DAY,12);
System.out.println(num2.getTime().toLocaleString());
num2.add(Calendar.HOUR_OF_DAY,1);
System.out.println(num2.getTime().toLocaleString());
int max = num1.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = num1.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max);
System.out.println(min);
}
}
- 注意:里面有已经被淘汰的方法(Date这个类里面的toLocalString)但还是可以用的