在實(shí)際項(xiàng)目當(dāng)中,我們經(jīng)常會(huì)涉及到對(duì)時(shí)間的處理,例如登陸網(wǎng)站,我們會(huì)看到網(wǎng)站首頁(yè)顯示XXX,歡迎您!今天是XXXX年。。。。某些網(wǎng)站會(huì)記錄下用戶登陸的時(shí)間,比如銀行的一些網(wǎng)站,對(duì)于這些經(jīng)常需要處理的問(wèn)題,Java中提供了Calendar這個(gè)專門用于對(duì)日期進(jìn)行操作的類,那么這個(gè)類有什么特殊的地方呢,首先我們來(lái)看Calendar的聲明
1
|
public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar> |
該類被abstract所修飾,說(shuō)明不能通過(guò)new的方式來(lái)獲得實(shí)例,對(duì)此,Calendar提供了一個(gè)類方法getInstance,以獲得此類型的一個(gè)通用的對(duì)象,getInstance方法返回一個(gè)Calendar對(duì)象(該對(duì)象為Calendar的子類對(duì)象),其日歷字段已由當(dāng)前日期和時(shí)間初始化:
1
|
Calendar rightNow = Calendar.getInstance(); |
為什么說(shuō)返回的是Calendar的子類對(duì)象呢,因?yàn)槊總€(gè)國(guó)家地區(qū)都有自己的一套日歷算法,比如西方國(guó)家的第一個(gè)星期大部分為星期日,而中國(guó)則為星期一,我們來(lái)看看getInstance方法獲取實(shí)例的源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * Gets a calendar using the default time zone and locale. The * <code>Calendar</code> returned is based on the current time * in the default time zone with the default locale. * * @return a Calendar. */ public static Calendar getInstance() { Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)); cal.sharedZone = true ; return cal; } |
其中createCalendar方法就是根據(jù)不同國(guó)家地區(qū)返回對(duì)應(yīng)的日期子類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
private static Calendar createCalendar(TimeZone zone, Locale aLocale) { Calendar cal = null ; String caltype = aLocale.getUnicodeLocaleType( "ca" ); if (caltype == null ) { // Calendar type is not specified. // If the specified locale is a Thai locale, // returns a BuddhistCalendar instance. if ( "th" .equals(aLocale.getLanguage()) && ( "TH" .equals(aLocale.getCountry()))) { cal = new BuddhistCalendar(zone, aLocale); } else { cal = new GregorianCalendar(zone, aLocale); } } else if (caltype.equals( "japanese" )) { cal = new JapaneseImperialCalendar(zone, aLocale); } else if (caltype.equals( "buddhist" )) { cal = new BuddhistCalendar(zone, aLocale); } else { // Unsupported calendar type. // Use Gregorian calendar as a fallback. cal = new GregorianCalendar(zone, aLocale); } return cal; } |
為了更加便捷的對(duì)日期進(jìn)行操作,Calendar類對(duì)YEAR、MONTH、DAY_OF_MONTH、HOUR等日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來(lái)表示,它是距歷元(即格林威治標(biāo)準(zhǔn)時(shí)間 1970 年 1 月 1 日的 00:00:00.000,格里高利歷)的偏移量。
下面看看Calendar常用的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package com.test.calendar; import java.util.Calendar; import org.junit.Before; import org.junit.Test; public class CalendarDemo { Calendar calendar = null ; @Before public void test() { calendar = Calendar.getInstance(); } // 基本用法,獲取年月日時(shí)分秒星期 @Test public void test1() { // 獲取年 int year = calendar.get(Calendar.YEAR); // 獲取月,這里需要需要月份的范圍為0~11,因此獲取月份的時(shí)候需要+1才是當(dāng)前月份值 int month = calendar.get(Calendar.MONTH) + 1 ; // 獲取日 int day = calendar.get(Calendar.DAY_OF_MONTH); // 獲取時(shí) int hour = calendar.get(Calendar.HOUR); // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小時(shí)表示 // 獲取分 int minute = calendar.get(Calendar.MINUTE); // 獲取秒 int second = calendar.get(Calendar.SECOND); // 星期,英語(yǔ)國(guó)家星期從星期日開(kāi)始計(jì)算 int weekday = calendar.get(Calendar.DAY_OF_WEEK); System.out.println( "現(xiàn)在是" + year + "年" + month + "月" + day + "日" + hour + "時(shí)" + minute + "分" + second + "秒" + "星期" + weekday); } // 一年后的今天 @Test public void test2() { // 同理?yè)Q成下個(gè)月的今天calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.YEAR, 1 ); // 獲取年 int year = calendar.get(Calendar.YEAR); // 獲取月 int month = calendar.get(Calendar.MONTH) + 1 ; // 獲取日 int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.println( "一年后的今天:" + year + "年" + month + "月" + day + "日" ); } // 獲取任意一個(gè)月的最后一天 @Test public void test3() { // 假設(shè)求6月的最后一天 int currentMonth = 6 ; // 先求出7月份的第一天,實(shí)際中這里6為外部傳遞進(jìn)來(lái)的currentMonth變量 // 1 calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1 ); calendar.add(Calendar.DATE, - 1 ); // 獲取日 int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.println( "6月份的最后一天為" + day + "號(hào)" ); } // 設(shè)置日期 @Test public void test4() { calendar.set(Calendar.YEAR, 2000 ); System.out.println( "現(xiàn)在是" + calendar.get(Calendar.YEAR) + "年" ); calendar.set( 2008 , 8 , 8 ); // 獲取年 int year = calendar.get(Calendar.YEAR); // 獲取月 int month = calendar.get(Calendar.MONTH); // 獲取日 int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.println( "現(xiàn)在是" + year + "年" + month + "月" + day + "日" ); } } |
程序輸出結(jié)果:
1
2
3
4
5
|
現(xiàn)在是 2016 年 11 月 7 日 11 時(shí) 42 分 18 秒星期 2 一年后的今天: 2017 年 11 月 7 日 6 月份的最后一天為 30 號(hào) 現(xiàn)在是 2000 年 現(xiàn)在是 2008 年 8 月 8 日 |
Calendar類中也有before,after,compareTo等方法,用法與Date類的類似,只是現(xiàn)在推薦用Calendar類操作日期。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://www.cnblogs.com/huangminwen/p/6041168.html