本文講的java日期計算比較偏,用到的地方很少(比如獲取今天所在周的周一或者周日,獲取今天是本月的第幾周...),這些方法是以前做項目遺留下來的,現在整理一下,跟大家分享。
工具類主要有一下方法:
public static date getfirstmondayofmonth(string datestring, string dateformat) throws exception
獲取指定月份的第一個星期一,比如2014-12 月的第一個周一是2014-12-01
public static int figureweekindexofmonth(string datestring, string dateformat) throws exception
計算指定時間屬于月份中的第幾周,比如2014-12月的第一周是1號到7號,那么2014-12-05 就是12月的第一周,2014-12-12 就是第二周
public static string getmondyoftoday(string format)
獲取今天所在周的星期一, 返回一個時間字符串。 如今天是2014-12-8,那么返回的是: 2014-12-08 (今天剛好是本周周一)
public static date getsundayoftoday()
獲取今天所在周的星期天, 如今天是2014-12-8,那么返回的是 2014-12-14
下面是工具類的詳細代碼:
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.date; import java.util.list; /** * @文件名稱 :dateutil.java * @所在包 :com.nerve.human.common.util * @功能描述 : * 時間格式工具類 * @創建者 :集成顯卡 [email protected] * @公司:ibm gdc * @創建日期 :2013-4-9 * @log : */ public class dateutil { public static date todate(string timestring, string format) throws exception{ return new simpledateformat(format).parse(timestring); } /** * * @method name: tostring * @return type: string * @param date * @param format * @return */ public static string tostring(date date, string format){ string strtime = null ; try { simpledateformat simpledateformat = new simpledateformat(format); strtime = simpledateformat.format(date); } catch (exception ex) { system.err.println( "格式化日期錯誤 : " + ex.getmessage()); } return strtime; } /** * 獲取當月的第一個星期一(以中國為例) * @method name: getfirstmonday * @return type: void */ public static date getfirstmondayofmonth(string month) throws exception{ return getfirstmondayofmonth(month, "yyyy-mm" ); } /** * 獲取當月的第一個星期一(以中國為例) * @method name: getfirstmonday * @return type: void */ public static date getfirstmondayofmonth(string datestring, string dateformat) throws exception{ date date = todate(datestring, dateformat); calendar c = calendar.getinstance(); c.settime(date); int step = ( 9 - c.get(calendar.day_of_week)) % 7 ; c.add(calendar.day_of_year, step); return c.gettime(); } /** * 計算指定時間屬于月份中的第幾周 * 比如2014-12月的第一周是1號到7號 * 那么2014-12-05 就是12月的第一周 * 2014-12-12 就是第二周 * * @method name: figureweekindexofmonth * @return type: int * * @param date * @return */ public static int figureweekindexofmonth(string datestring, string dateformat) throws exception{ calendar c = calendar.getinstance(); date curdate = todate(datestring, dateformat); c.settime(curdate); int day = c.get(calendar.day_of_month); simpledateformat sdf = new simpledateformat( "yyyy-mm" ); date firstmondy = getfirstmondayofmonth(sdf.format(c.gettime())); c.settime(firstmondy); int index = 0 ; do { c.add(calendar.day_of_month, 7 ); index ++; } while (c.get(calendar.day_of_month) < day); return index; } /** * 獲取今天所在周的星期一 * @method name: getmondyoftoday * @return type: string * * @return */ public static string getmondyoftoday(string format){ calendar c = calendar.getinstance(); int step = c.get(calendar.day_of_week); //星期天 if (step == 1 ) step = 6 ; else step -= 2 ; c.add(calendar.day_of_year, -step); return tostring(c.gettime(), format); } /** * 獲取今天所在周的星期天 * @method name: getmondyoftoday * @return type: string * * @return */ public static date getsundayoftoday(){ calendar c = calendar.getinstance(); int step = c.get(calendar.day_of_week); if (step != calendar.sunday) c.add(calendar.day_of_year, 8 -step); return c.gettime(); } /** * 獲取指定時間所在的星期天 * @param date * @return */ public static date getsundayofdate(date date){ calendar c = calendar.getinstance(); c.settime(date); int step = c.get(calendar.day_of_week); if (step != calendar.sunday) c.add(calendar.day_of_year, 8 -step); return c.gettime(); } } |
來個測試截圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/ssrc0604hx/article/details/41800623