本文實例為大家分享了java日期操作工具類,獲取指定日期前一天、后一天;日期轉換;兩個日期之間相隔天數等工具類,供大家參考,具體內容如下
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class DateUtil { /** * 獲得指定日期的前一天 * * @param specifiedDay * @param format * @return * @throws Exception */ public static Date getDayBefore(Date date, String format) { Calendar c = Calendar.getInstance(); c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day - 1 ); String dayBeforeStr = new SimpleDateFormat(format).format(c.getTime()); Date dayBefore = null ; try { dayBefore = new SimpleDateFormat(format).parse(dayBeforeStr); } catch (ParseException e) { e.printStackTrace(); } return dayBefore; } /** * 獲得指定日期的后一天 * * @param specifiedDay * @return */ public static Date getDayAfter(Date date, String format) { Calendar c = Calendar.getInstance(); c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day + 1 ); String dayAfterStr = new SimpleDateFormat(format).format(c.getTime()); Date dayAfter = null ; try { dayAfter = new SimpleDateFormat(format).parse(dayAfterStr); } catch (ParseException e) { e.printStackTrace(); } return dayAfter; } /** * 獲得指定日期的前一天 * * @param specifiedDay * @param format * @return * @throws Exception */ public static String getSpecifiedDayBefore(String specifiedDay, String format) { // SimpleDateFormat simpleDateFormat = new // SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); Date date = null ; try { date = new SimpleDateFormat( "yy-MM-dd" ).parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day - 1 ); String dayBefore = new SimpleDateFormat( "yyyy-MM-dd" ).format(c.getTime()); return dayBefore; } /** * 獲得指定日期的后一天 * * @param specifiedDay * @return */ public static String getSpecifiedDayAfter(String specifiedDay, String format) { Calendar c = Calendar.getInstance(); Date date = null ; try { // date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); date = new SimpleDateFormat(format).parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day + 1 ); // String dayAfter=new // SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); String dayAfter = new SimpleDateFormat(format).format(c.getTime()); return dayAfter; } /** * 將date類型準成指定format格式的字符串 * * @param day 日期 * @param format 指定格式 * @return */ public static String date2String(Date day, String format) { String dateStr = new SimpleDateFormat(format).format(day.getTime()); return dateStr; } /** * 將字符串轉成指定格式的date類型 * * @param day 日期 * @param format 指定格式 * @return */ public static Date string2Date(String dateStr,String format) { Date strDate = null ; try { strDate = new SimpleDateFormat(format).parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return strDate; } /** * * @Title: DateDate2CompareValue * @Description: 兩個date類型比較大小 * @param dayAfter date類型 * @param date date類型 * @param 格式化 * @return boolean * @throws */ public static boolean DateDate2CompareValue(Date dayAfter, Date date, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { Date d1 = sdf.parse(sdf.format(dayAfter)); Date d2 = sdf.parse(sdf.format(date)); if (d1.getTime()>=d2.getTime()){ return true ; } } catch (ParseException e) { e.printStackTrace(); } return false ; } /** * 比較兩個日期年月日的大小 * @Title: DateCompareValue * @Description: 一個stirng一個date類型比較大小 * @param date1 String類型 * @param date2 date類型 * @param @return * @return boolean * @throws */ public static boolean DateStrDateCompareValue(String date1, Date date, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { Date d1 = sdf.parse(date1); Date d2 = sdf.parse(sdf.format(date)); if (d1.getTime()>=d2.getTime()){ return true ; } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false ; } /** * 比較兩個日期年月日的大小 * @Title: DateStr2CompareValue * @Description: 兩個string類型比較時間大小 * @param date1 * @param date2 * @return boolean * @throws */ @SuppressWarnings ( "unused" ) public static boolean DateStr2CompareValue(String date1, String date2,String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { Date d1 = sdf.parse(date1); Date d2 = sdf.parse(date2); if (d1.getTime()>=d2.getTime()){ return true ; } } catch (ParseException e) { e.printStackTrace(); } return false ; } /** * * @Title: get2DateListDate * @date 2016年5月17日 下午2:11:48 * @Description: 獲取時間之內的 相隔天數的date集合 * @param @param preDate 開始時間 * @param @param nextDate 結束時間 * @param @param format fomat格式 * @param @return * @return List<Date> 相隔天數集合 * @throws */ public static List<Date> get2DateListDate(String preDate, String nextDate, String format) { List<Date> list = new ArrayList<>(); if (nextDate.equals(preDate)){ //開始時間 結束時間相等就直接返回 list.add(DateUtil.string2Date(preDate, format)); return list; } String nexDate = DateUtil.getSpecifiedDayAfter(preDate, format); list.add(DateUtil.string2Date(preDate, format)); list.add(DateUtil.string2Date(nexDate, format)); while (!nexDate.equals(nextDate)) { String nextnextDate = DateUtil.getSpecifiedDayAfter(nexDate, format); list.add(DateUtil.string2Date(nextnextDate, format)); nexDate = nextnextDate; } return list; } /** * * @Title: get2DateListDate * @date 2016年5月17日 下午2:11:48 * @Description: 獲取時間之內的 相隔天數的string集合 * @param @param preDate 開始時間 * @param @param nextDate 結束時間 * @param @param format fomat格式 * @param @return * @return List<Date> 相隔天數集合 * @throws */ public static List<String> get2DateListDateStr(String preDate, String nextDate, String format) { List<String> list = new ArrayList<>(); if (nextDate.equals(preDate)){ //如果開始時間等于結束時間那么就是同一天 list.add(preDate); return list; } String nexDate = DateUtil.getSpecifiedDayAfter(preDate, format); list.add(preDate); list.add(nexDate); while (!nexDate.equals(nextDate)) { String nextnextDate = DateUtil.getSpecifiedDayAfter(nexDate, format); list.add(nextnextDate); nexDate = nextnextDate; } return list; } /** * * @Title: get2DateListWithDate * @date 2016年5月26日 上午9:20:29 * @Description: 獲取兩個日期之間日期的 * @param @param startDate 開始日期 date類型 * @param @param endDate 結束日期 date類型 * @param @return * @return List<Date> datelist * @throws */ public static List<Date> get2DateListWithDate(Date startDate, Date endDate,String format) { List<Date> list = new ArrayList<>(); if (startDate.getTime() >endDate.getTime()){ return list; } String startDateStr = DateUtil.date2String(startDate, format); String endDateStr = DateUtil.date2String(endDate, format); if (startDateStr.equals(endDateStr)){ //如果開始時間等于結束時間那么就是同一天 list.add(startDate); return list; } Date nextDate = DateUtil.getDayAfter(startDate, format); String nextDateStr = DateUtil.date2String(nextDate, format); //String endDateStr = DateUtil.date2String(endDate, format); list.add(startDate); list.add(nextDate); while (!nextDateStr.equals(endDateStr)) { String nextnextDate = DateUtil.getSpecifiedDayAfter(nextDateStr, format); list.add(DateUtil.string2Date(nextnextDate, format)); nextDateStr = nextnextDate; } return list; } public static void main(String[] args) throws Exception { /**/ String preDate = getSpecifiedDayBefore( "2016-05-01" , "yyyy-MM-dd" ); String nextDate = getSpecifiedDayAfter( "2016-05-03" , "yyyy-MM-dd" ); Date befroDay = getDayBefore( new Date(), "yyyy-MM-dd" ); Date dateAfter = getDayAfter( new Date(), "yyyy-MM-dd" ); // System.out.println("前一天:" + preDate + " 后一天:" + nextDate); // System.err.println("前一天:" +date2String( befroDay ,"yyyy-MM-dd")+ " // 后一天:" + dateAfter); String mat = "yyyy-MM-dd" ; // 這里最好從數據庫中讀取 SimpleDateFormat dateTimeFormat = new SimpleDateFormat(mat); Date dd = DateUtil.getDayBefore( new Date(), mat); // Date befroDay = getDayBefore(new Date(), "yyyy-MM-dd"); String sd = date2String(befroDay, "yyyy-MM-dd" ); String dateStr = dateTimeFormat.format(befroDay); // System.out.println("dateStr="+dateStr+" sd "+sd); //List<Date> listDate = get2DateListDate("2016-05-01", "2016-05-03", "yyyy-MM-dd"); //List<String> listDateStr = get2DateListDateStr("2016-05-01", "2016-05-03", "yyyy-MM-dd"); Date startDate = new Date(); Date endDate = dateTimeFormat.parse( "2016-05-31" ); List<Date> listDate = get2DateListWithDate(startDate,endDate, "yyyy-MM-dd" ); for ( int i = 0 ; i < listDate.size(); i++) { System.err.println(listDate.get(i)); } /*for (int i = 0; i < listDateStr.size(); i++) { //System.out.println(listDateStr.get(i)); }*/ } |
以上就是本文的全部內容,希望對大家的學習java程序設計有所幫助。