java-Calendar类

1、Calendar类的概述和获取日期的方法
  * A:Calendar类的概述
    * Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
  * B:成员方法
    * public static Calendar getInstance()
    * public int get(int field)


2、Calendar类的add()和set()方法
  * A:成员方法
    * public void add(int field,int amount)
    * public final void set(int year,int month,int date)

例:

 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 }
原文地址:https://www.cnblogs.com/hfumin/p/10195320.html