我们需要知道Calendar类是一个抽象类,不可以被实例化,但是我们查看API可知它的子类为GregorianCalendar,继承了它的全部方法,我们在使用时和SimpleDateFormat类一样,都是父类引用指向子类对象,从而在进行成员方法等的调用,下面我们来看一下示例:
import java.util.Calendar;
public class Demo8_Calendar {
public static void main(String[] args) {
}
public static void demo2() {
Calendar c = Calendar.getInstance();
System.out.println("更改后的日期为:"+c.get(Calendar.YEAR)+"年"+getNum(c.get(Calendar.MONTH))+"月"
+getNum(c.get(Calendar.DAY_OF_MONTH))+"日"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static void demo1() {
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println("当前日期为:"+c.get(Calendar.YEAR)+"年"+getNum(c.get(Calendar.MONTH))+"月"
+getNum(c.get(Calendar.DAY_OF_MONTH))+"日"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getWeek(int week) {
String []arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return arr[week];
}
public static String getNum(int num) {
return num > 9 ? ""+num : "0"+num;
}
}