Java Calendar類的使用總結

  在實際項目當中,咱們常常會涉及到對時間的處理,例如登錄網站,咱們會看到網站首頁顯示XXX,歡迎您!今天是XXXX年。。。。某些網站會記錄下用戶登錄的時間,好比銀行的一些網站,對於這些常常須要處理的問題,Java中提供了Calendar這個專門用於對日期進行操做的類,那麼這個類有什麼特殊的地方呢,首先咱們來看Calendar的聲明html

public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>

  該類被abstract所修飾,說明不能經過new的方式來得到實例,對此,Calendar提供了一個類方法getInstance,以得到此類型的一個通用的對象,getInstance方法返回一個Calendar對象(該對象爲Calendar的子類對象),其日曆字段已由當前日期和時間初始化:java

Calendar rightNow = Calendar.getInstance();

  爲何說返回的是Calendar的子類對象呢,由於每一個國家地區都有本身的一套日曆算法,好比西方國家的第一個星期大部分爲星期日,而中國則爲星期一,咱們來看看getInstance方法獲取實例的源碼算法

 1 /**
 2  * Gets a calendar using the default time zone and locale. The
 3  * <code>Calendar</code> returned is based on the current time
 4  * in the default time zone with the default locale.
 5  *
 6  * @return a Calendar.
 7  */
 8 public static Calendar getInstance()
 9 {
10     Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
11     cal.sharedZone = true;
12     return cal;
13 }

其中createCalendar方法就是根據不一樣國家地區返回對應的日期子類網站

 1 private static Calendar createCalendar(TimeZone zone,
 2                                            Locale aLocale)
 3     {
 4         Calendar cal = null;
 5 
 6         String caltype = aLocale.getUnicodeLocaleType("ca");
 7         if (caltype == null) {
 8             // Calendar type is not specified.
 9             // If the specified locale is a Thai locale,
10             // returns a BuddhistCalendar instance.
11             if ("th".equals(aLocale.getLanguage())
12                     && ("TH".equals(aLocale.getCountry()))) {
13                 cal = new BuddhistCalendar(zone, aLocale);
14             } else {
15                 cal = new GregorianCalendar(zone, aLocale);
16             }
17         } else if (caltype.equals("japanese")) {
18             cal = new JapaneseImperialCalendar(zone, aLocale);
19         } else if (caltype.equals("buddhist")) {
20             cal = new BuddhistCalendar(zone, aLocale);
21         } else {
22             // Unsupported calendar type.
23             // Use Gregorian calendar as a fallback.
24             cal = new GregorianCalendar(zone, aLocale);
25         }
26 
27         return cal;
28     }

  爲了更加便捷的對日期進行操做,Calendar類對YEAR、MONTH、DAY_OF_MONTH、HOUR等日曆字段之間的轉換提供了一些方法,併爲操做日曆字段(例如得到下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距曆元(即格林威治標準時間 1970 年 1 月 1 日的 00:00:00.000,格里高利曆)的偏移量。spa

下面看看Calendar經常使用的方法code

 1 package com.test.calendar;
 2 
 3 import java.util.Calendar;
 4 
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 
 8 public class CalendarDemo {
 9     Calendar calendar = null;
10 
11     @Before
12     public void test() {
13         calendar = Calendar.getInstance();
14     }
15 
16     // 基本用法,獲取年月日時分秒星期
17     @Test
18     public void test1() {
19         // 獲取年
20         int year = calendar.get(Calendar.YEAR);
21 
22         // 獲取月,這裏須要須要月份的範圍爲0~11,所以獲取月份的時候須要+1纔是當前月份值
23         int month = calendar.get(Calendar.MONTH) + 1;
24 
25         // 獲取日
26         int day = calendar.get(Calendar.DAY_OF_MONTH);
27 
28         // 獲取時
29         int hour = calendar.get(Calendar.HOUR);
30         // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小時表示
31 
32         // 獲取分
33         int minute = calendar.get(Calendar.MINUTE);
34 
35         // 獲取秒
36         int second = calendar.get(Calendar.SECOND);
37 
38         // 星期,英語國家星期從星期日開始計算
39         int weekday = calendar.get(Calendar.DAY_OF_WEEK);
40 
41         System.out.println("如今是" + year + "年" + month + "月" + day + "日" + hour
42                 + "時" + minute + "分" + second + "秒" + "星期" + weekday);
43     }
44 
45     // 一年後的今天
46     @Test
47     public void test2() {
48         // 同理換成下個月的今天calendar.add(Calendar.MONTH, 1);
49         calendar.add(Calendar.YEAR, 1);
50 
51         // 獲取年
52         int year = calendar.get(Calendar.YEAR);
53 
54         // 獲取月
55         int month = calendar.get(Calendar.MONTH) + 1;
56 
57         // 獲取日
58         int day = calendar.get(Calendar.DAY_OF_MONTH);
59 
60         System.out.println("一年後的今天:" + year + "年" + month + "月" + day + "日");
61     }
62 
63     // 獲取任意一個月的最後一天
64     @Test
65     public void test3() {
66         // 假設求6月的最後一天
67         int currentMonth = 6;
68         // 先求出7月份的第一天,實際中這裏6爲外部傳遞進來的currentMonth變量
69         // 1
70         calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);
71 
72         calendar.add(Calendar.DATE, -1);
73 
74         // 獲取日
75         int day = calendar.get(Calendar.DAY_OF_MONTH);
76 
77         System.out.println("6月份的最後一天爲" + day + "號");
78     }
79 
80     // 設置日期
81     @Test
82     public void test4() {
83         calendar.set(Calendar.YEAR, 2000);
84         System.out.println("如今是" + calendar.get(Calendar.YEAR) + "年");
85 
86         calendar.set(2008, 8, 8);
87         // 獲取年
88         int year = calendar.get(Calendar.YEAR);
89 
90         // 獲取月
91         int month = calendar.get(Calendar.MONTH);
92 
93         // 獲取日
94         int day = calendar.get(Calendar.DAY_OF_MONTH);
95 
96         System.out.println("如今是" + year + "年" + month + "月" + day + "日");
97     }
98 }

程序輸出結果:htm

1 如今是2016年11月7日11時42分18秒星期2
2 一年後的今天:2017年11月7日
3 6月份的最後一天爲30號
4 如今是2000年
5 如今是2008年8月8日

Calendar類中也有before,after,compareTo等方法,用法與Date類的相似,只是如今推薦用Calendar類操做日期。對象

相關文章
相關標籤/搜索