Java Calendar 類時間操作,這也許是創建日歷和管理最簡單的一個方案,示范代碼很簡單,演示了獲取時間,日期時間的累加和累減,以及比較。
注意事項:
Calendar 的 month 從 0 開始,也就是全年 12 個月由 0 ~ 11 進行表示。
而 Calendar.DAY_OF_WEEK 定義和值如下:
Calendar.SUNDAY = 1
Calendar.MONDAY = 2
Calendar.TUESDAY = 3
Calendar.WEDNESDAY = 4
Calendar.THURSDAY = 5
Calendar.FRIDAY = 6
Calendar.SATURDAY = 7
SimpleDateFormat 的格式定義
Java 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
package demo; import java.util.Date; import java.text.SimpleDateFormat; import java.text.DateFormat; import java.text.ParseException; import java.util.Calendar; public class Test { public Test() { } public static void main(String[] args) { // 字符串轉換日期格式 // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 接收傳入參數 // String strDate = args[1]; // 得到日期格式對象 // Date date = fmtDateTime.parse(strDate); // 完整顯示今天日期時間 String str = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss:SSS" )).format( new Date()); System.out.println(str); // 創建 Calendar 對象 Calendar calendar = Calendar.getInstance(); try { // 對 calendar 設置時間的方法 // 設置傳入的時間格式 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-M-d H:m:s" ); // 指定一個日期 Date date = dateFormat.parse( "2013-6-1 13:24:16" ); // 對 calendar 設置為 date 所定的日期 calendar.setTime(date); // 按特定格式顯示剛設置的時間 str = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss:SSS" )).format(calendar.getTime()); System.out.println(str); } catch (ParseException e) { e.printStackTrace(); } // 或者另一種設置 calendar 方式 // 分別爲 year, month, date, hourOfDay, minute, second calendar = Calendar.getInstance(); calendar.set( 2013 , 1 , 2 , 17 , 35 , 44 ); str = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss:SSS" )).format(calendar.getTime()); System.out.println(str); // Calendar 取得當前時間的方法 // 初始化 (重置) Calendar 對象 calendar = Calendar.getInstance(); // 或者用 Date 來初始化 Calendar 對象 calendar.setTime( new Date()); // setTime 類似上面一行 // Date date = new Date(); // calendar.setTime(date); str = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss:SSS" )).format(calendar.getTime()); System.out.println(str); // 顯示年份 int year = calendar.get(Calendar.YEAR); System.out.println( "year is = " + String.valueOf(year)); // 顯示月份 (從0開始, 實際顯示要加一) int month = calendar.get(Calendar.MONTH); System.out.println( "nth is = " + (month + 1 )); // 本周幾 int week = calendar.get(Calendar.DAY_OF_WEEK); System.out.println( "week is = " + week); // 今年的第 N 天 int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR); System.out.println( "DAY_OF_YEAR is = " + DAY_OF_YEAR); // 本月第 N 天 int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH); System.out.println( "DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH)); // 3小時以后 calendar.add(Calendar.HOUR_OF_DAY, 3 ); int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY); System.out.println( "HOUR_OF_DAY + 3 = " + HOUR_OF_DAY); // 當前分鐘數 int MINUTE = calendar.get(Calendar.MINUTE); System.out.println( "MINUTE = " + MINUTE); // 15 分鐘以后 calendar.add(Calendar.MINUTE, 15 ); MINUTE = calendar.get(Calendar.MINUTE); System.out.println( "MINUTE + 15 = " + MINUTE); // 30分鐘前 calendar.add(Calendar.MINUTE, - 30 ); MINUTE = calendar.get(Calendar.MINUTE); System.out.println( "MINUTE - 30 = " + MINUTE); // 格式化顯示 str = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss:SS" )).format(calendar.getTime()); System.out.println(str); // 重置 Calendar 顯示當前時間 calendar.setTime( new Date()); str = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss:SS" )).format(calendar.getTime()); System.out.println(str); // 創建一個 Calendar 用于比較時間 Calendar calendarNew = Calendar.getInstance(); // 設定為 5 小時以前,后者大,顯示 -1 calendarNew.add(Calendar.HOUR, - 5 ); System.out.println( "時間比較:" + calendarNew.compareTo(calendar)); // 設定7小時以后,前者大,顯示 1 calendarNew.add(Calendar.HOUR, + 7 ); System.out.println( "時間比較:" + calendarNew.compareTo(calendar)); // 退回 2 小時,時間相同,顯示 0 calendarNew.add(Calendar.HOUR, - 2 ); System.out.println( "時間比較:" + calendarNew.compareTo(calendar)); } } |
要計算時間差,可用 Calendar.getTimeInMillis() 取得兩個時間的微秒級的時間差,再加以換算即可,比如獲得相差天數,代碼如下:
1
2
3
4
|
// 得微秒級時間差 long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis(); // 換算后得到天數 long day = val / ( 1000 * 60 * 60 * 24 ); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。