java Calendar類
/**
* 測(cè)試Calendar類日歷抽象類 和 GregorianCalendar類公歷子類
*/
public class TestDate2 {
? ?public static void main(String[] args) {
? ? ? ?Calendar calendar = new GregorianCalendar(2000,11,1,23,0,5);
? ? ? ?//2000年 12月??!注意0代表1月 11代表12月 ?1號(hào) 23點(diǎn) 0分 5秒 ? 輸入數(shù)字比實(shí)際月份小1個(gè)月 取值范圍從0-11
? ? ? ?System.out.println(calendar.getTime()+"的結(jié)果為Fri Dec 01 23:00:05 CST 2000");
? ? ? ?//Dec對(duì)應(yīng)11
? ? ? ?Date d1 = calendar.getTime();
? ? ? ?//.getTime()返回Date類對(duì)象
? ? ? ?calendar.setTime(new Date());
? ? ? ?//將Date對(duì)象的值賦給calendar
? ? ? ?GregorianCalendar gc2 = new GregorianCalendar(2010,0,5,10,20,30);
? ? ? ?System.out.println(gc2.getTime()+"的結(jié)果為周二Tue Jan 05 10:20:30 CST 2010");
? ? ? ?int year = gc2.get(Calendar.YEAR);
? ? ? ?//.get(int field)返回 field所代表的屬性的值 返回值為int field年月日等屬性對(duì)應(yīng)不同的int值 通過(guò)Calendar.常量直接調(diào)用 結(jié)果2010
? ? ? ?int month = gc2.get(Calendar.MONTH);
? ? ? ?//結(jié)果 0 ?0+1一月 .get()返回int
? ? ? ?int week = gc2.get(Calendar.WEEK_OF_MONTH);
? ? ? ?//月中第幾周 結(jié)果2
? ? ? ?int day = gc2.get(Calendar.DAY_OF_MONTH);
? ? ? ?//月中幾號(hào) 結(jié)果5號(hào)
? ? ? ?int dayOfWeek = gc2.get(Calendar.DAY_OF_WEEK);
? ? ? ?System.out.println(dayOfWeek+"的結(jié)果為3 周二對(duì)應(yīng)3");
? ? ? ?//day of week 的2對(duì)應(yīng)周一 ?1對(duì)應(yīng)周日 7對(duì)應(yīng)周六 輸出周幾比實(shí)際周幾大一天 取值范圍1-7
? ? ? ?gc2.set(Calendar.YEAR,2022);
? ? ? ?//.set(int field, int value) 給哪個(gè)field賦值value 和get(field)一樣可以通過(guò)內(nèi)置的常量調(diào)用
? ? ? ?gc2.set(Calendar.MONTH,Calendar.OCTOBER);
? ? ? ?//每個(gè)月也有對(duì)應(yīng)的常量
? ? ? ?gc2.set(Calendar.DAY_OF_MONTH,20);
? ? ? ?gc2.set(Calendar.HOUR_OF_DAY,20);
? ? ? ?gc2.set(Calendar.MINUTE,20);
? ? ? ?gc2.set(Calendar.SECOND,30);
? ? ? ?SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss");
? ? ? ?//通過(guò)SimpleDateFormat構(gòu)造器規(guī)定日期顯示格式
? ? ? ?System.out.println(df.format(gc2.getTime())+"的結(jié)果為2022年10月20日 星期四 08:20:30");
? ? ? ?//df.format(Date date) ? gc2.getTime()返回Date
? ?}
}
class MyCalendar{
? ?public static void printCalendar(Calendar c){
? ? ? ?int year = c.get(Calendar.YEAR);
? ? ? ?int month = c.get(Calendar.MONTH)+1;
? ? ? ?//用中文表述需要+1
? ? ? ?int day = c.get(Calendar.DAY_OF_MONTH);
? ? ? ?int dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-1;
? ? ? ?//用中文表述需要-1 原本是1對(duì)應(yīng)周日 7對(duì)應(yīng)周六 沒有0 ?-1后變成1-6對(duì)應(yīng)周一到周六 0對(duì)應(yīng)周日
? ? ? ?int hour = c.get(Calendar.HOUR_OF_DAY);
? ? ? ?int minute = c.get(Calendar.MINUTE);
? ? ? ?int second = c.get(Calendar.SECOND);
? ? ? ?System.out.printf("%d年%d月%d日",year,month,day);
? ? ? ?//%d占位符代表數(shù)字 字符串中每一個(gè)占位符對(duì)應(yīng)后邊的一個(gè)參數(shù) %s代表字符串
? ? ? ?switch(dayOfWeek){
? ? ? ? ? ?case 0:
? ? ? ? ? ? ? ?System.out.print("周日");
? ? ? ? ? ? ? ?//將int的周幾打印漢字
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 1:
? ? ? ? ? ? ? ?System.out.print("周一");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 2:
? ? ? ? ? ? ? ?System.out.print("周二");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 3:
? ? ? ? ? ? ? ?System.out.print("周三");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 4:
? ? ? ? ? ? ? ?System.out.print("周四");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 5:
? ? ? ? ? ? ? ?System.out.print("周五");
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 6:
? ? ? ? ? ? ? ?System.out.print("周六");
? ? ? ? ? ? ? ?break;
? ? ? ?}
? ? ? ?System.out.printf("%d:%d:%d",hour,minute,second);
? ?}
? ?public static void main(String[] args) {
? ? ? ?GregorianCalendar gc = new GregorianCalendar();
? ? ? ?//無(wú)參生成當(dāng)下時(shí)刻
? ? ? ?printCalendar(gc);
? ? ? ?//結(jié)果2022年7月12日周二13:9:0
? ?}
}