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