package com.heima.stringbuffer;
import java.util.Calendar;
public class Demo12_Calendar {
/*
* A:Calendar类的概述
* Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
* B:成员方法
* public static Calendar getInstance()
* public int get(int field)
*/
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
System.out.println(c.get(Calendar.YEAR)+ "年" + (c.get(Calendar.MONTH) + 1) + "月" + c.get(Calendar.DAY_OF_MONTH)
+ "日" + getWeek(Calendar.DAY_OF_WEEK));
Calendar c2 = Calendar.getInstance(); //父类引用指向子类对象
System.out.println(c.get(Calendar.YEAR)+ "年" + (getNum(c.get(Calendar.MONTH) + 1)) + "月" +
(getNum(c.get(Calendar.DAY_OF_MONTH))+ "日" + getWeek(Calendar.DAY_OF_WEEK)));
}
/*将星期存储表中进行查表
* 1,返回值类型String
* 2,参数列表int week
*/
public static String getWeek(int week) {
String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return arr[week];
}
/*
* 如果是个数数字前面补0
* 1,返回值类型String
* 2,参数类表int num
*/
public static String getNum(int num) {
return num >9 ? "" + num : "0" + num;
}
}
|
|