一、Calendar類的概述和獲取日期的方法
* A:Calendar類的概述
* Calendar 類是一個抽象類,它爲特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日曆字段之間的轉換提供了一些方法,併爲操做日曆字段(例如得到下星期的日期)提供了一些方法。
* B:成員方法
* public static Calendar getInstance()
* public int get(int field)spa
二、Calendar類的add()和set()方法
* A:成員方法
* public void add(int field,int amount)
* public final void set(int year,int month,int date)code
例:blog
1 public class Demo { 2 3 public static void main(String[] args) { 4 //demo1(); 5 Calendar c = Calendar.getInstance(); 6 // c.add(Calendar.MONTH, -1); 7 // c.set(Calendar.YEAR, 2008); 8 c.set(2000,7,8); 9 System.out.println(c.get(Calendar.YEAR) + "年" 10 + getNum(c.get(Calendar.MONTH) + 1) + "月" 11 + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" 12 + getWeek(c.get(Calendar.DAY_OF_WEEK))); 13 } 14 15 private static void demo1() { 16 Calendar c = Calendar.getInstance(); 17 // System.out.println(c); 18 System.out.println(c.get(Calendar.YEAR)); 19 System.out.println(c.get(Calendar.MONTH)); 20 System.out.println(c.get(Calendar.DAY_OF_MONTH)); 21 System.out.println(c.get(Calendar.YEAR) + "年" 22 + getNum(c.get(Calendar.MONTH) + 1) + "月" 23 + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" 24 + getWeek(c.get(Calendar.DAY_OF_WEEK))); 25 } 26 27 public static String getWeek(int week) { 28 String[] arr = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 29 30 return arr[week]; 31 } 32 33 public static String getNum(int num) { 34 // if(num>9){ 35 // return ""+num; 36 // }else{ 37 // return "0"+num; 38 // } 39 40 return num > 9 ? "" + num : "0" + num; 41 } 42 43 }