提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
|
|
3 |
import java.text.ParseException; |
|
4 |
import java.text.SimpleDateFormat; |
|
5 |
import java.util.Calendar; |
|
6 |
import java.util.Date; |
|
7 |
import java.util.Locale; |
|
8 |
|
|
9 |
public class DateUtil { |
|
10 |
|
|
11 |
private static SimpleDateFormat Format_1 = new SimpleDateFormat("yyyyMMdd"); |
|
12 |
private static SimpleDateFormat Format_2 = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
13 |
private static SimpleDateFormat Format_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
14 |
private static SimpleDateFormat Format_4 = new SimpleDateFormat("yyyy-MM-dd"); |
|
15 |
private static SimpleDateFormat Format_5 = new SimpleDateFormat("HH:mm:ss"); |
|
16 |
private static SimpleDateFormat Format_6 = new SimpleDateFormat("yyyyMM"); |
|
17 |
private static SimpleDateFormat Format_7 = new SimpleDateFormat("MM月dd日 HH:mm"); |
|
18 |
private static SimpleDateFormat Format_8 = new SimpleDateFormat("yyyy年MM月"); |
|
19 |
private static SimpleDateFormat Format_9 = new SimpleDateFormat("MM-dd"); |
|
20 |
private static SimpleDateFormat Format_10 = new SimpleDateFormat("yyyy年MM月dd日"); |
|
21 |
private static SimpleDateFormat Format_11 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); |
|
22 |
private static SimpleDateFormat Format_12 = new SimpleDateFormat("yyyyMMddHHmm"); |
|
23 |
private static SimpleDateFormat Format_13 = new SimpleDateFormat("yyyy/MM/dd"); |
|
24 |
private static SimpleDateFormat Format_14 = new SimpleDateFormat("yyyy-MM"); |
|
25 |
private static SimpleDateFormat Format_15 = new SimpleDateFormat("yyyyMMddHHmmssSSS"); |
|
26 |
private static SimpleDateFormat Format_16 = new SimpleDateFormat("yyyy/MM/dd HH:mm"); |
|
27 |
private static SimpleDateFormat Format_17 = new SimpleDateFormat("HH:mm"); |
|
28 |
|
894d12
|
29 |
/**时间格式转化iso8601 |
C |
30 |
* @param date 时间 |
|
31 |
* @return 返回的时间格式字符串 |
|
32 |
*/ |
|
33 |
public static String dateFormatISO8601(Date date) { |
|
34 |
if(!SimpleTool.checkNotNull(date)){ |
|
35 |
return ""; |
|
36 |
} |
|
37 |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");//设置日期格式 |
|
38 |
return df.format(date); |
|
39 |
} |
|
40 |
|
5c5945
|
41 |
/**时间格式转化 |
E |
42 |
* @param date 时间 |
|
43 |
* @param format 时间格式 |
|
44 |
* @return 返回的时间格式字符串 |
|
45 |
*/ |
|
46 |
public static String dateFormat(Date date, String format) { |
|
47 |
if(!SimpleTool.checkNotNull(date)){ |
|
48 |
return ""; |
|
49 |
} |
|
50 |
SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式 |
|
51 |
return df.format(date); |
|
52 |
} |
|
53 |
|
f2a234
|
54 |
/**时间戳转时间 |
C |
55 |
* @param timestamp 时间戳 |
|
56 |
* @param format 时间格式 |
|
57 |
* @return 返回的时间格式字符串 |
|
58 |
*/ |
|
59 |
public static Date timestampToDate(long timestamp, String format) { |
|
60 |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
61 |
String sd = sdf.format(new Date(timestamp)); // 时间戳转换成时间 |
|
62 |
return DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss"); |
|
63 |
} |
|
64 |
|
5c5945
|
65 |
/** |
E |
66 |
* 转换成yyyyMMdd格式的日期字符串 |
|
67 |
* |
|
68 |
* @param date |
|
69 |
* @return |
|
70 |
*/ |
|
71 |
public static String formatDate(Date date) { |
|
72 |
return Format_1.format(date); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* 转换成yyyyMMddHHmmss格式的日期字符串 |
|
77 |
* |
|
78 |
* @param date |
|
79 |
* @return |
|
80 |
*/ |
|
81 |
public static String formatDate_1(Date date) { |
|
82 |
return Format_2.format(date); |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* 转换成yyyy-MM-dd HH:mm:ss格式的日期字符串 |
|
87 |
* |
|
88 |
* @param date |
|
89 |
* @return |
|
90 |
*/ |
|
91 |
public static String formatDate_2(Date date) { |
|
92 |
return date == null ? null : Format_3.format(date); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* 转换成yyyy-MM-dd格式的日期字符串 |
|
97 |
* |
|
98 |
* @param date |
|
99 |
* @return |
|
100 |
*/ |
|
101 |
public static String formatDate_3(Date date) { |
|
102 |
return date != null ? Format_4.format(date) : null; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* 转换成HH:mm:ss格式的日期字符串 |
|
107 |
* |
|
108 |
* @param date |
|
109 |
* @return |
|
110 |
*/ |
|
111 |
public static String formatDate_4(Date date) { |
|
112 |
return Format_5.format(date); |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* 转换成yyyyMM格式的日期字符串 |
|
117 |
* |
|
118 |
* @param date |
|
119 |
* @return |
|
120 |
*/ |
|
121 |
public static String formatDate_5(Date date) { |
|
122 |
return Format_6.format(date); |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* 转换成mm月dd日 HH:mm格式的日期字符串 |
|
127 |
* |
|
128 |
* @param date |
|
129 |
* @return |
|
130 |
*/ |
|
131 |
public static String formatDate_6(Date date) { |
|
132 |
return Format_7.format(date); |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* 转换成yyyy年mm月格式的日期字符串 |
|
137 |
* |
|
138 |
* @param date |
|
139 |
* @return |
|
140 |
*/ |
|
141 |
public static String formatDate_7(Date date) { |
|
142 |
return Format_8.format(date); |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* 转换成mm-dd格式的日期字符串 |
|
147 |
* |
|
148 |
* @param date |
|
149 |
* @return |
|
150 |
*/ |
|
151 |
public static String formatDate_8(Date date) { |
|
152 |
return Format_9.format(date); |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* 转换成yyyy年MM月dd日格式的日期字符串 |
|
157 |
* |
|
158 |
* @param date |
|
159 |
* @return |
|
160 |
*/ |
|
161 |
public static String formatDate_9(Date date) { |
|
162 |
return Format_10.format(date); |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* 转换成yyyyMMddHHmm格式的日期字符串 |
|
167 |
* |
|
168 |
* @param date |
|
169 |
* @return |
|
170 |
*/ |
|
171 |
public static String formatDate_11(Date date) { |
|
172 |
return Format_12.format(date); |
|
173 |
} |
|
174 |
|
|
175 |
/** |
|
176 |
* 转换成yyyy年MM月dd日 HH:mm:ss格式的日期字符串 |
|
177 |
* |
|
178 |
* @param date |
|
179 |
* @return |
|
180 |
*/ |
|
181 |
public static String formatDate_10(Date date) { |
|
182 |
return Format_11.format(date); |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 转换成yyyy/MM/dd 格式的日期字符串 |
|
187 |
* |
|
188 |
* @param date |
|
189 |
* @return |
|
190 |
*/ |
|
191 |
public static String formatDate_13(Date date) { |
|
192 |
return Format_13.format(date); |
|
193 |
} |
|
194 |
|
|
195 |
/** |
|
196 |
* 转换成yyyy-MM 格式的日期字符串 |
|
197 |
* |
|
198 |
* @param date |
|
199 |
* @return |
|
200 |
*/ |
|
201 |
public static String formatDate_14(Date date) { |
|
202 |
return Format_14.format(date); |
|
203 |
} |
|
204 |
|
1ffbaa
|
205 |
|
W |
206 |
/** |
|
207 |
* 当前时间之前的时间与当前时间相差多少秒 |
|
208 |
* @param startDate 当前时间之前的时间 |
|
209 |
* @return |
|
210 |
*/ |
|
211 |
public static int calLastedTime(Date startDate) { |
|
212 |
long nowDate = new Date().getTime(); |
|
213 |
long startDateTime = startDate.getTime(); |
|
214 |
int diffSeconds = (int) ((nowDate - startDateTime) / 1000); |
|
215 |
return diffSeconds; |
|
216 |
} |
|
217 |
|
|
218 |
|
5c5945
|
219 |
/** |
E |
220 |
* 转换成yyyyMMddHHmmssSSS格式的日期字符串 |
|
221 |
* |
|
222 |
* @param date |
|
223 |
* @return |
|
224 |
*/ |
|
225 |
public static String formatDate_15(Date date) { |
|
226 |
return Format_15.format(date); |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* 转换成yyyy/MM/dd HH:mm格式的日期字符串 |
|
231 |
* |
|
232 |
* @param date |
|
233 |
* @return |
|
234 |
*/ |
|
235 |
public static String formatDate_16(Date date) { |
|
236 |
return Format_16.format(date); |
|
237 |
} |
|
238 |
|
|
239 |
/** |
|
240 |
* 转换成HH:mm格式的日期字符串 |
|
241 |
* |
|
242 |
* @param date |
|
243 |
* @return |
|
244 |
*/ |
|
245 |
public static String formatDate_17(Date date) { |
|
246 |
return Format_17.format(date); |
|
247 |
} |
|
248 |
|
|
249 |
/** |
|
250 |
* 转换成字符串yyyyMMddHHmmss成日�? |
|
251 |
* |
|
252 |
* @param str |
|
253 |
* @return |
|
254 |
* @throws Exception |
|
255 |
*/ |
190d78
|
256 |
public static Date parseString(String str) { |
E |
257 |
try { |
|
258 |
return Format_2.parse(str); |
|
259 |
}catch (Exception e) |
|
260 |
{ |
|
261 |
return null; |
|
262 |
} |
5c5945
|
263 |
} |
E |
264 |
|
|
265 |
/** |
|
266 |
* 转换成字符串到指定的日期 |
|
267 |
* |
|
268 |
* @param str |
|
269 |
* @param format |
|
270 |
* @return |
|
271 |
* @throws Exception |
|
272 |
*/ |
190d78
|
273 |
public static Date parseString(String str, String format) { |
5c5945
|
274 |
SimpleDateFormat sdf = new SimpleDateFormat(format); |
190d78
|
275 |
try{ |
E |
276 |
return sdf.parse(str); |
|
277 |
}catch (Exception e) |
|
278 |
{ |
|
279 |
return null; |
|
280 |
} |
5c5945
|
281 |
} |
E |
282 |
|
cd868c
|
283 |
/**字符串转成时间yyyy-MM-dd HH:mm:ss*/ |
E |
284 |
public static Date parseString_1(String str) |
|
285 |
{ |
|
286 |
try{ |
|
287 |
return Format_3.parse(str); |
|
288 |
}catch (Exception e) |
|
289 |
{ |
|
290 |
return null; |
|
291 |
} |
|
292 |
} |
|
293 |
|
|
294 |
/**字符串转成时间yyyy-MM-dd*/ |
|
295 |
public static Date parseString_2(String str) |
|
296 |
{ |
|
297 |
try{ |
|
298 |
return Format_4.parse(str); |
|
299 |
}catch (Exception e) |
|
300 |
{ |
|
301 |
return null; |
|
302 |
} |
|
303 |
} |
|
304 |
|
1daac0
|
305 |
/**时间上秒叠加 |
C |
306 |
* @Author: ChenJiaHe |
|
307 |
* @param dateTime 时间 |
|
308 |
* @param second 秒 |
|
309 |
* @return |
|
310 |
*/ |
|
311 |
public static Date addSecond(Date dateTime,int second){ |
|
312 |
Calendar c = Calendar.getInstance(); |
|
313 |
c.setTime(dateTime); |
|
314 |
c.add(Calendar.SECOND, second); |
|
315 |
return c.getTime(); |
|
316 |
} |
cd868c
|
317 |
|
5c5945
|
318 |
/**时间上分钟叠加 |
E |
319 |
* @Author: ChenJiaHe |
|
320 |
* @param dateTime 时间 |
|
321 |
* @param min 分钟 |
|
322 |
* @return |
|
323 |
*/ |
|
324 |
public static Date addMin(Date dateTime,int min){ |
|
325 |
Calendar c = Calendar.getInstance(); |
|
326 |
c.setTime(dateTime); |
|
327 |
c.add(Calendar.MINUTE, min); |
|
328 |
return c.getTime(); |
|
329 |
} |
|
330 |
|
|
331 |
/**时间上小时叠加 |
|
332 |
* @Author: ChenJiaHe |
|
333 |
* @param dateTime 时间 |
|
334 |
* @param hour 小时 |
|
335 |
* @return |
|
336 |
*/ |
|
337 |
public static Date addhour(Date dateTime,int hour){ |
|
338 |
Calendar c = Calendar.getInstance(); |
|
339 |
c.setTime(dateTime); |
|
340 |
c.add(Calendar.HOUR, hour); |
|
341 |
return c.getTime(); |
|
342 |
} |
|
343 |
|
|
344 |
/**时间上天数叠加 |
|
345 |
* @Author: ChenJiaHe |
|
346 |
* @param dateTime 时间 |
|
347 |
* @param dayNum 天数 |
|
348 |
* @return |
|
349 |
*/ |
|
350 |
public static Date addDay(Date dateTime,int dayNum){ |
|
351 |
Calendar c = Calendar.getInstance(); |
|
352 |
c.setTime(dateTime); |
|
353 |
c.add(Calendar.DATE, dayNum); |
|
354 |
return c.getTime(); |
|
355 |
} |
|
356 |
|
76b630
|
357 |
/**时间上月数叠加 |
C |
358 |
* @Author: ChenJiaHe |
|
359 |
* @param dateTime 时间 |
|
360 |
* @param dayNum 天数 |
|
361 |
* @return |
|
362 |
*/ |
|
363 |
public static Date addMonth(Date dateTime,int dayNum){ |
|
364 |
Calendar c = Calendar.getInstance(); |
|
365 |
c.setTime(dateTime); |
|
366 |
c.add(Calendar.MONTH, dayNum); |
|
367 |
return c.getTime(); |
|
368 |
} |
|
369 |
|
f06126
|
370 |
/**时间上年数叠加 |
C |
371 |
* @Author: ChenJiaHe |
|
372 |
* @param dateTime 时间 |
|
373 |
* @param dayNum 天数 |
|
374 |
* @return |
|
375 |
*/ |
|
376 |
public static Date addYear(Date dateTime,int dayNum){ |
|
377 |
Calendar c = Calendar.getInstance(); |
|
378 |
c.setTime(dateTime); |
|
379 |
c.add(Calendar.YEAR, dayNum); |
|
380 |
return c.getTime(); |
|
381 |
} |
|
382 |
|
5c5945
|
383 |
/** |
E |
384 |
* 转换成字符串到指定的日期 |
|
385 |
* |
|
386 |
* @param date |
|
387 |
* @param format |
|
388 |
* @return |
|
389 |
* @throws Exception |
|
390 |
*/ |
|
391 |
public static String formatDate(Date date, String format) { |
|
392 |
if (date == null) { |
|
393 |
return null; |
|
394 |
} |
|
395 |
|
|
396 |
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA); |
|
397 |
return sdf.format(date); |
|
398 |
} |
|
399 |
|
|
400 |
/**日期转星期 |
|
401 |
* @param dateTime 字符串时间 |
|
402 |
* @param format 字符串时间格式 |
|
403 |
* @param returnType 返回类型 0星期几1周几 |
|
404 |
* @return 数据 |
|
405 |
*/ |
|
406 |
public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException { |
|
407 |
SimpleDateFormat f = new SimpleDateFormat(format); |
|
408 |
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; |
|
409 |
if(SimpleTool.checkNotNull(returnType)&&returnType==1){ |
|
410 |
weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; |
|
411 |
} |
|
412 |
Calendar cal = Calendar.getInstance(); // 获得一个日历 |
|
413 |
Date datet = null; |
|
414 |
datet = f.parse(dateTime); |
|
415 |
cal.setTime(datet); |
|
416 |
|
|
417 |
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。 |
|
418 |
if (w < 0) |
|
419 |
w = 0; |
|
420 |
return weekDays[w]; |
|
421 |
} |
|
422 |
|
|
423 |
/**日期转星期 |
|
424 |
* @param dateTime 时间 |
|
425 |
* @param returnType 返回类型 0星期几1周几 |
|
426 |
* @return 数据 |
|
427 |
*/ |
|
428 |
public static String dateToWeek(Date dateTime,Integer returnType) { |
|
429 |
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; |
|
430 |
if(SimpleTool.checkNotNull(returnType)&&returnType==1){ |
|
431 |
weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; |
|
432 |
} |
|
433 |
Calendar cal = Calendar.getInstance(); // 获得一个日历 |
|
434 |
cal.setTime(dateTime); |
|
435 |
|
|
436 |
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。 |
|
437 |
if (w < 0) |
|
438 |
w = 0; |
|
439 |
return weekDays[w]; |
|
440 |
} |
|
441 |
|
db7fc9
|
442 |
/** |
336175
|
443 |
* 获取两个时间相差分钟数 |
A |
444 |
* @param startTime 开始时间 |
|
445 |
* @param endTime 结束时间 |
|
446 |
* @return 分钟 |
|
447 |
*/ |
|
448 |
public static long differMinute(Date startTime, Date endTime) { |
|
449 |
long NTime = startTime.getTime(); |
|
450 |
long OTime = endTime.getTime(); |
|
451 |
return (NTime - OTime) / 1000 / 60; |
|
452 |
} |
|
453 |
|
|
454 |
/** |
db7fc9
|
455 |
* endTime比startTime多的天数 |
C |
456 |
* @param startTime 最小时间 |
|
457 |
* @param endTime 最大时间 |
578f7b
|
458 |
* @return 返回 |
db7fc9
|
459 |
*/ |
C |
460 |
public static Integer differDay(Date startTime,Date endTime){ |
|
461 |
Calendar cal1 = Calendar.getInstance(); |
|
462 |
cal1.setTime(startTime); |
|
463 |
|
|
464 |
Calendar cal2 = Calendar.getInstance(); |
|
465 |
cal2.setTime(endTime); |
|
466 |
int day1= cal1.get(Calendar.DAY_OF_YEAR); |
|
467 |
int day2 = cal2.get(Calendar.DAY_OF_YEAR); |
|
468 |
|
|
469 |
int year1 = cal1.get(Calendar.YEAR); |
|
470 |
int year2 = cal2.get(Calendar.YEAR); |
|
471 |
if(year1 != year2) { //同一年 |
|
472 |
int timeDistance = 0 ; |
|
473 |
for(int i = year1 ; i < year2 ; i ++) { |
|
474 |
if(i%4==0 && i%100!=0 || i%400==0) {//闰年 |
|
475 |
timeDistance += 366; |
|
476 |
} |
|
477 |
else {//不是闰年 |
|
478 |
timeDistance += 365; |
|
479 |
} |
|
480 |
} |
|
481 |
|
|
482 |
return timeDistance + (day2-day1) ; |
|
483 |
} |
|
484 |
else { //不同年 |
|
485 |
System.out.println("判断day2 - day1 : " + (day2-day1)); |
|
486 |
return day2-day1; |
|
487 |
} |
|
488 |
} |
|
489 |
|
5c5945
|
490 |
/**判断两个时间是不是同一天*/ |
E |
491 |
public static boolean timeEqual(Date startTime,Date endTime){ |
|
492 |
if(startTime == null || endTime==null){ |
|
493 |
return false; |
|
494 |
} |
|
495 |
boolean status = false; |
|
496 |
if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){ |
6f2306
|
497 |
status = true; |
5c5945
|
498 |
} |
E |
499 |
return status; |
|
500 |
} |
|
501 |
|
63b0b0
|
502 |
/**把秒转换成X天X时X分X秒*/ |
578f7b
|
503 |
public static String getChineseStr(Integer second) { |
63b0b0
|
504 |
int day = 24 * 60 * 60; |
E |
505 |
int hour = 60 * 60; |
|
506 |
int min = 60; |
|
507 |
|
|
508 |
int dayNum = second / day; |
|
509 |
int hourNum = second % day / hour; |
|
510 |
int minNum = second % day % hour / min; |
|
511 |
second = second % day % hour % min; |
|
512 |
|
|
513 |
String str = dayNum > 0 ? dayNum + "天" : ""; |
|
514 |
str += hourNum > 0 ? hourNum + "时" : ""; |
|
515 |
str += minNum > 0 ? minNum + "分" : ""; |
0ff9b8
|
516 |
str += second + "秒"; |
63b0b0
|
517 |
|
E |
518 |
return str; |
|
519 |
} |
|
520 |
|
387efa
|
521 |
/** |
E |
522 |
* 针对str格式的时间做转换 格式为"xx:xx" |
|
523 |
* @param time 传入的时间 |
|
524 |
* @return 返回分钟如果10:25,则返回625 |
|
525 |
*/ |
|
526 |
public static int getMinuteNum(String time){ |
|
527 |
|
|
528 |
if(!StringUtils.isEmpty(time)) |
|
529 |
{ |
|
530 |
String[] arr = time.split(":"); |
|
531 |
if(arr != null && arr.length == 2) |
|
532 |
{ |
|
533 |
return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]); |
|
534 |
} |
|
535 |
} |
|
536 |
|
|
537 |
return 0; |
2e1c15
|
538 |
} |
387efa
|
539 |
|
2e1c15
|
540 |
/** |
C |
541 |
* 获取当前月的开始时间 |
|
542 |
* @param time 时间 |
|
543 |
* @return 返回时间 格式yyyy-MM-dd 00:00:00 |
|
544 |
*/ |
|
545 |
public static Date getMonthStart(Date time) { |
|
546 |
Calendar calendar = Calendar.getInstance(); |
|
547 |
calendar.setTime(time); |
|
548 |
calendar.set(Calendar.DAY_OF_MONTH, 1); |
|
549 |
calendar.set(Calendar.HOUR_OF_DAY, 0); |
|
550 |
calendar.set(Calendar.MINUTE, 0); |
|
551 |
calendar.set(Calendar.SECOND, 0); |
f610e6
|
552 |
calendar.set(Calendar.MILLISECOND,0); |
2e1c15
|
553 |
return calendar.getTime(); |
387efa
|
554 |
} |
E |
555 |
|
ba61c9
|
556 |
/** |
F |
557 |
* 获取当前月的开始时间 |
|
558 |
* |
|
559 |
* @param num 0拿取当月,正代表后,负代表前,值为几个(月) |
|
560 |
* @return 返回时间 格式yyyy-MM-dd 00:00:00 |
|
561 |
*/ |
|
562 |
public static String getMonthStart(Integer num) { |
|
563 |
Calendar calendar = Calendar.getInstance(); |
|
564 |
calendar.add(Calendar.MONTH, num); |
|
565 |
calendar.set(Calendar.DAY_OF_MONTH, 1); |
|
566 |
calendar.set(Calendar.HOUR_OF_DAY, 0); |
|
567 |
calendar.set(Calendar.MINUTE, 0); |
|
568 |
calendar.set(Calendar.SECOND, 0); |
|
569 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
|
570 |
return sdf.format(calendar.getTime()); |
2e1c15
|
571 |
} |
C |
572 |
|
|
573 |
/** |
|
574 |
* 获取当前月的结束时间 |
ca698f
|
575 |
* @param date 点前时间 |
c2bb89
|
576 |
* @return 返回时间 格式yyyy-MM-dd 23:59:59999 |
2e1c15
|
577 |
*/ |
C |
578 |
public static Date getMonthEnd(Date date) { |
|
579 |
Calendar calendar = Calendar.getInstance(); |
|
580 |
calendar.setTime(date); |
|
581 |
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
|
582 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
|
583 |
calendar.set(Calendar.MINUTE, 59); |
|
584 |
calendar.set(Calendar.SECOND, 59); |
f610e6
|
585 |
calendar.set(Calendar.MILLISECOND,999); |
2e1c15
|
586 |
return calendar.getTime(); |
ba61c9
|
587 |
} |
F |
588 |
|
|
589 |
/** |
85234c
|
590 |
* 获取当前月的结束时间,没有毫秒 |
C |
591 |
* @param date 点前时间 |
|
592 |
* @return 返回时间 格式yyyy-MM-dd 23:59:59 |
|
593 |
*/ |
|
594 |
public static Date getMonthEndNoMillisecond(Date date) { |
|
595 |
Calendar calendar = Calendar.getInstance(); |
|
596 |
calendar.setTime(date); |
|
597 |
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
|
598 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
|
599 |
calendar.set(Calendar.MINUTE, 59); |
|
600 |
calendar.set(Calendar.SECOND, 59); |
|
601 |
return calendar.getTime(); |
|
602 |
} |
|
603 |
|
|
604 |
/** |
ba61c9
|
605 |
* 获取当前月的结束时间 |
F |
606 |
* @param num 0拿取当月,正代表后,负代表前,值为几个(月) |
|
607 |
* @return 返回时间 格式yyyy-MM-dd 23:59:59 |
|
608 |
*/ |
|
609 |
public static String getMonthEnd(Integer num) { |
|
610 |
Calendar calendar = Calendar.getInstance(); |
|
611 |
calendar.add(Calendar.MONTH, num); |
|
612 |
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
|
613 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
|
614 |
calendar.set(Calendar.MINUTE, 59); |
|
615 |
calendar.set(Calendar.SECOND, 59); |
|
616 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
|
617 |
return sdf.format(calendar.getTime()); |
|
618 |
} |
|
619 |
|
5bfcd3
|
620 |
/** |
C |
621 |
* 获取当前年的开始时间 |
|
622 |
* @param time 时间 |
|
623 |
* @return 返回时间 格式yyyy-MM-dd 00:00:00 |
|
624 |
*/ |
|
625 |
public static Date getYearStart(Date time) { |
|
626 |
Calendar calendar = Calendar.getInstance(); |
|
627 |
calendar.setTime(time); |
a7fa7d
|
628 |
calendar.set(Calendar.MONTH, 0); |
5bfcd3
|
629 |
calendar.set(Calendar.DAY_OF_MONTH, 1); |
C |
630 |
calendar.set(Calendar.HOUR_OF_DAY, 0); |
|
631 |
calendar.set(Calendar.MINUTE, 0); |
|
632 |
calendar.set(Calendar.SECOND, 0); |
f610e6
|
633 |
calendar.set(Calendar.MILLISECOND,0); |
5bfcd3
|
634 |
return calendar.getTime(); |
C |
635 |
} |
|
636 |
|
ba61c9
|
637 |
/** |
ca698f
|
638 |
* 获取当前年的结束时间 |
C |
639 |
* @param time 时间 |
c2bb89
|
640 |
* @return 返回时间 格式yyyy-MM-dd 23:59:59999 |
ca698f
|
641 |
*/ |
C |
642 |
public static Date getYearEnd(Date time) { |
|
643 |
Calendar calendar = Calendar.getInstance(); |
|
644 |
calendar.setTime(time); |
a7fa7d
|
645 |
calendar.set(Calendar.MONTH, 11); |
ca698f
|
646 |
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE)); |
C |
647 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
|
648 |
calendar.set(Calendar.MINUTE, 59); |
|
649 |
calendar.set(Calendar.SECOND, 59); |
f610e6
|
650 |
calendar.set(Calendar.MILLISECOND,999); |
ca698f
|
651 |
return calendar.getTime(); |
C |
652 |
} |
|
653 |
|
85234c
|
654 |
/** |
C |
655 |
* 获取当前年的结束时间,没有毫秒 |
|
656 |
* @param time 时间 |
|
657 |
* @return 返回时间 格式yyyy-MM-dd 23:59:59 |
|
658 |
*/ |
|
659 |
public static Date getYearEndNoMillisecond(Date time) { |
|
660 |
Calendar calendar = Calendar.getInstance(); |
|
661 |
calendar.setTime(time); |
|
662 |
calendar.set(Calendar.MONTH, 11); |
|
663 |
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE)); |
|
664 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
|
665 |
calendar.set(Calendar.MINUTE, 59); |
|
666 |
calendar.set(Calendar.SECOND, 59); |
|
667 |
return calendar.getTime(); |
|
668 |
} |
|
669 |
|
ca698f
|
670 |
|
C |
671 |
/**这天的开始时间 |
ba61c9
|
672 |
* 日期2000-01-01变2000-01-01 00:00:00 |
F |
673 |
*/ |
|
674 |
public static String dayToStart(Date date) { |
|
675 |
Calendar calendar = Calendar.getInstance(); |
|
676 |
calendar.setTime(date); |
05b175
|
677 |
calendar.set(Calendar.HOUR_OF_DAY, 0); |
ba61c9
|
678 |
calendar.set(Calendar.MINUTE, 0); |
F |
679 |
calendar.set(Calendar.SECOND, 0); |
|
680 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
|
681 |
return sdf.format(calendar.getTime()); |
|
682 |
} |
|
683 |
|
ca698f
|
684 |
/**这天的最后时间 |
ba61c9
|
685 |
* 日期2000-01-01变2000-01-01 23:59:59 |
F |
686 |
*/ |
|
687 |
public static String dayToEnd(Date date) { |
|
688 |
Calendar calendar = Calendar.getInstance(); |
|
689 |
calendar.setTime(date); |
05b175
|
690 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
ba61c9
|
691 |
calendar.set(Calendar.MINUTE, 59); |
F |
692 |
calendar.set(Calendar.SECOND, 59); |
|
693 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
|
694 |
return sdf.format(calendar.getTime()); |
5bfcd3
|
695 |
} |
C |
696 |
|
5acd00
|
697 |
/**这天的开始时间 |
C |
698 |
* 日期2000-01-01变2000-01-01 00:00:00 |
|
699 |
*/ |
|
700 |
public static Date dayToStartDate(Date date) { |
|
701 |
Calendar calendar = Calendar.getInstance(); |
|
702 |
calendar.setTime(date); |
|
703 |
calendar.set(Calendar.HOUR_OF_DAY, 0); |
|
704 |
calendar.set(Calendar.MINUTE, 0); |
|
705 |
calendar.set(Calendar.SECOND, 0); |
f610e6
|
706 |
calendar.set(Calendar.MILLISECOND, 0); |
5acd00
|
707 |
return calendar.getTime(); |
C |
708 |
} |
|
709 |
|
ca698f
|
710 |
/**这天的最后时间 |
85234c
|
711 |
* 日期2000-01-01变2000-01-01 23:59:59999 |
5bfcd3
|
712 |
*/ |
C |
713 |
public static Date dayToEndDate(Date date) { |
|
714 |
Calendar calendar = Calendar.getInstance(); |
|
715 |
calendar.setTime(date); |
05b175
|
716 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
5bfcd3
|
717 |
calendar.set(Calendar.MINUTE, 59); |
C |
718 |
calendar.set(Calendar.SECOND, 59); |
f610e6
|
719 |
calendar.set(Calendar.MILLISECOND, 999); |
5bfcd3
|
720 |
return calendar.getTime(); |
ba61c9
|
721 |
} |
F |
722 |
|
85234c
|
723 |
/**这天的最后时间,没有毫秒 |
C |
724 |
* 日期2000-01-01变2000-01-01 23:59:59 |
|
725 |
*/ |
|
726 |
public static Date dayToEndDateNoMillisecond(Date date) { |
|
727 |
Calendar calendar = Calendar.getInstance(); |
|
728 |
calendar.setTime(date); |
|
729 |
calendar.set(Calendar.HOUR_OF_DAY, 23); |
|
730 |
calendar.set(Calendar.MINUTE, 59); |
|
731 |
calendar.set(Calendar.SECOND, 59); |
|
732 |
return calendar.getTime(); |
|
733 |
} |
|
734 |
|
2e1c15
|
735 |
/**获取月份的天数 |
C |
736 |
* @param date 时间 |
|
737 |
* @return 月份的天数 |
|
738 |
*/ |
|
739 |
public static int getMonthDays(Date date) { |
|
740 |
Calendar cal = Calendar.getInstance(); |
|
741 |
cal.setTime(date); |
|
742 |
cal.set(Calendar.DATE, 1); |
|
743 |
cal.roll(Calendar.DATE, -1); |
|
744 |
return cal.getActualMaximum(Calendar.DATE); |
|
745 |
} |
578f7b
|
746 |
|
2e1c15
|
747 |
/**获取月份的天数 |
C |
748 |
* @param year 年份 |
|
749 |
* @param month 月份 |
|
750 |
* @return 月份的天数 |
|
751 |
*/ |
|
752 |
public static int getMonthDays(int year, int month) { |
|
753 |
Calendar cal = Calendar.getInstance(); |
|
754 |
cal.set(Calendar.YEAR, year); |
|
755 |
cal.set(Calendar.MONTH, (month - 1)); |
|
756 |
cal.set(Calendar.DATE, 1); |
|
757 |
cal.roll(Calendar.DATE, -1); |
|
758 |
return cal.getActualMaximum(Calendar.DATE); |
|
759 |
} |
|
760 |
|
|
761 |
/**获取月份的天数 |
|
762 |
* @param yearMonth 年月 |
|
763 |
* @param format 时间格式 |
578f7b
|
764 |
* @return 返回 |
2e1c15
|
765 |
*/ |
C |
766 |
public static int getMonthDays(String yearMonth,String format) { |
|
767 |
SimpleDateFormat sdf = new SimpleDateFormat(format); |
|
768 |
Calendar calendar = Calendar.getInstance(); |
|
769 |
try { |
|
770 |
calendar.setTime(sdf.parse(yearMonth)); |
|
771 |
} catch (ParseException e) { |
|
772 |
e.printStackTrace(); |
|
773 |
} |
|
774 |
calendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
|
775 |
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
|
776 |
} |
5bfcd3
|
777 |
|
C |
778 |
/**当天多少点前 |
|
779 |
* @param date 时间 |
|
780 |
* @param num 多少点前,不含当点前,24小时制 |
|
781 |
* @return 月份的天数 |
|
782 |
*/ |
|
783 |
public static boolean getFrontMinute(Date date,int num) { |
|
784 |
Calendar cal = Calendar.getInstance(); |
|
785 |
cal.setTime(date); |
05b175
|
786 |
cal.set(Calendar.HOUR_OF_DAY, num-1); |
5bfcd3
|
787 |
cal.set(Calendar.MINUTE, 59); |
C |
788 |
cal.set(Calendar.SECOND, 59); |
|
789 |
|
|
790 |
return date.compareTo(cal.getTime()) < 1; |
|
791 |
} |
8782bc
|
792 |
|
F |
793 |
/** |
|
794 |
* 获得某个日期的当天某点 |
|
795 |
* 例如:2022-12-26 11:20:00 -> 2022-12-26 13:00:00 |
|
796 |
* @param num 24小时 |
|
797 |
*/ |
|
798 |
public static String getHourDayTime(Date date,int num) { |
|
799 |
Calendar calendar = Calendar.getInstance(); |
|
800 |
calendar.setTime(date); |
|
801 |
calendar.set(Calendar.HOUR_OF_DAY, num); |
|
802 |
calendar.set(Calendar.MINUTE, 0); |
|
803 |
calendar.set(Calendar.SECOND, 0); |
|
804 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
|
805 |
return sdf.format(calendar.getTime()); |
|
806 |
} |
217258
|
807 |
|
F |
808 |
/** |
|
809 |
* 获取时间当月剩余天数 |
|
810 |
* */ |
|
811 |
public static Integer getMonthSurplus(Date date) { |
|
812 |
Calendar month = Calendar.getInstance(); |
|
813 |
month.setTime(new Date()); |
|
814 |
month.set(Calendar.DATE, 1); |
|
815 |
month.roll(Calendar.DATE, -1); |
|
816 |
return month.getActualMaximum(Calendar.DATE) - Calendar.getInstance().get(Calendar.DAY_OF_MONTH); |
|
817 |
} |
3a93e8
|
818 |
|
F |
819 |
/**获取当前时间所在周的周一00:00:00*/ |
|
820 |
public static Date getMonday(Date date) { |
|
821 |
Calendar calendar = Calendar.getInstance(Locale.CHINA); |
|
822 |
calendar.setTime(date); |
|
823 |
//以周一为首日 |
|
824 |
calendar.setFirstDayOfWeek(Calendar.MONDAY); |
|
825 |
calendar.set(Calendar.HOUR_OF_DAY, 0); |
|
826 |
calendar.set(Calendar.MINUTE, 0); |
|
827 |
calendar.set(Calendar.SECOND, 0); |
|
828 |
//周一 |
|
829 |
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); |
|
830 |
return calendar.getTime(); |
|
831 |
} |
736fa6
|
832 |
|
G |
833 |
/** |
|
834 |
* 根据出生年月日计算年龄 |
|
835 |
* @param birth |
|
836 |
* @return |
|
837 |
*/ |
|
838 |
public static int getAge(Date birth) { |
|
839 |
Calendar cal = Calendar.getInstance(); |
|
840 |
int thisYear = cal.get(Calendar.YEAR); |
|
841 |
int thisMonth = cal.get(Calendar.MONTH); |
|
842 |
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |
|
843 |
|
|
844 |
cal.setTime(birth); |
|
845 |
int birthYear = cal.get(Calendar.YEAR); |
|
846 |
int birthMonth = cal.get(Calendar.MONTH); |
|
847 |
int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |
|
848 |
|
|
849 |
int age = thisYear - birthYear; |
|
850 |
|
|
851 |
// 未足月 |
|
852 |
if (thisMonth <= birthMonth) { |
|
853 |
// 当月 |
|
854 |
if (thisMonth == birthMonth) { |
|
855 |
// 未足日 |
|
856 |
if (dayOfMonth < birthdayOfMonth) { |
|
857 |
age--; |
|
858 |
} |
|
859 |
} else { |
|
860 |
age--; |
|
861 |
} |
|
862 |
} |
|
863 |
return age; |
|
864 |
} |
7381f4
|
865 |
|
F |
866 |
/** |
|
867 |
* 获取某天结束秒数 |
d55850
|
868 |
* @param dateTime 日期 |
F |
869 |
* @param lateSecond 延迟秒数 |
7381f4
|
870 |
* @return |
F |
871 |
*/ |
dca24a
|
872 |
public static long todayEndSecond(Date dateTime, Long lateSecond) { |
d55850
|
873 |
if(dateTime == null){ |
F |
874 |
dateTime = new Date(); |
7381f4
|
875 |
} |
d55850
|
876 |
if(lateSecond == null){ |
F |
877 |
lateSecond = 0L; |
|
878 |
} |
|
879 |
Date endTime = DateUtil.dayToEndDate(dateTime); |
|
880 |
return differSecond(dateTime, endTime) + lateSecond; |
7381f4
|
881 |
} |
F |
882 |
|
|
883 |
/** |
|
884 |
* 计算2个实际相差秒数 |
|
885 |
* @param startTime 开始时间 |
|
886 |
* @param endTime 结束时间 |
|
887 |
* @return |
|
888 |
*/ |
dca24a
|
889 |
public static long differSecond(Date startTime, Date endTime) { |
7381f4
|
890 |
if(startTime == null || endTime == null){ |
F |
891 |
return 0L; |
|
892 |
} |
|
893 |
long sTime = startTime.getTime(); |
|
894 |
long eTime = endTime.getTime(); |
|
895 |
return (eTime - sTime) / 1000L; |
|
896 |
} |
5c5945
|
897 |
} |