在實際項目當中,我們經常會涉及到對時間的處理,例如登陸網站,我們會看到網站首頁顯示xxx,歡迎您!今天是xxxx年。。。。某些網站會記錄下用戶登陸的時間,比如銀行的一些網站,對于這些經常需要處理的問題,java中提供了calendar這個專門用于對日期進行操作的類,那么這個類有什么特殊的地方呢,首先我們來看calendar的聲明
1
|
public abstract class calendar extends objectimplements serializable, cloneable, comparable<calendar> |
該類被abstract所修飾,說明不能通過new的方式來獲得實例,對此,calendar提供了一個類方法getinstance,以獲得此類型的一個通用的對象,getinstance方法返回一個calendar對象(該對象為calendar的子類對象),其日歷字段已由當前日期和時間初始化:
1
|
calendar rightnow = calendar.getinstance(); |
為什么說返回的是calendar的子類對象呢,因為每個國家地區都有自己的一套日歷算法,比如西方國家的第一個星期大部分為星期日,而中國則為星期一,我們來看看getinstance方法獲取實例的源碼
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方法就是根據不同國家地區返回對應的日期子類
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; } |
為了更加便捷的對日期進行操作,calendar類對year、month、day_of_month、hour等日歷字段之間的轉換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距歷元(即格林威治標準時間 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(); } // 基本用法,獲取年月日時分秒星期 @test public void test1() { // 獲取年 int year = calendar.get(calendar.year); // 獲取月,這里需要需要月份的范圍為0~11,因此獲取月份的時候需要+1才是當前月份值 int month = calendar.get(calendar.month) + 1 ; // 獲取日 int day = calendar.get(calendar.day_of_month); // 獲取時 int hour = calendar.get(calendar.hour); // int hour = calendar.get(calendar.hour_of_day); // 24小時表示 // 獲取分 int minute = calendar.get(calendar.minute); // 獲取秒 int second = calendar.get(calendar.second); // 星期,英語國家星期從星期日開始計算 int weekday = calendar.get(calendar.day_of_week); system.out.println( "現在是" + year + "年" + month + "月" + day + "日" + hour + "時" + minute + "分" + second + "秒" + "星期" + weekday); } // 一年后的今天 @test public void test2() { // 同理換成下個月的今天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 + "日" ); } // 獲取任意一個月的最后一天 @test public void test3() { // 假設求6月的最后一天 int currentmonth = 6 ; // 先求出7月份的第一天,實際中這里6為外部傳遞進來的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 + "號" ); } // 設置日期 @test public void test4() { calendar.set(calendar.year, 2000 ); system.out.println( "現在是" + 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( "現在是" + year + "年" + month + "月" + day + "日" ); } } |
程序輸出結果:
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類操作日期
以上所述是小編給大家介紹的java calendar類的使用總結實例詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/huangminwen/p/6041168.html