cc
2021-10-19 73cd66f49ca2261449b85fb6bdcd2439031d8fd8
提交 | 用户 | 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
29     /**时间格式转化
30      * @param date 时间
31      * @param format 时间格式
32      * @return 返回的时间格式字符串
33      */
34     public static String dateFormat(Date date, String format) {
35         if(!SimpleTool.checkNotNull(date)){
36             return "";
37         }
38         SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
39         return df.format(date);
40     }
41
42     /**
43      * 转换成yyyyMMdd格式的日期字符串
44      *
45      * @param date
46      * @return
47      */
48     public static String formatDate(Date date) {
49         return Format_1.format(date);
50     }
51
52     /**
53      * 转换成yyyyMMddHHmmss格式的日期字符串
54      *
55      * @param date
56      * @return
57      */
58     public static String formatDate_1(Date date) {
59         return Format_2.format(date);
60     }
61
62     /**
63      * 转换成yyyy-MM-dd HH:mm:ss格式的日期字符串
64      *
65      * @param date
66      * @return
67      */
68     public static String formatDate_2(Date date) {
69         return date == null ? null : Format_3.format(date);
70     }
71
72     /**
73      * 转换成yyyy-MM-dd格式的日期字符串
74      *
75      * @param date
76      * @return
77      */
78     public static String formatDate_3(Date date) {
79         return date != null ? Format_4.format(date) : null;
80     }
81
82     /**
83      * 转换成HH:mm:ss格式的日期字符串
84      *
85      * @param date
86      * @return
87      */
88     public static String formatDate_4(Date date) {
89         return Format_5.format(date);
90     }
91
92     /**
93      * 转换成yyyyMM格式的日期字符串
94      *
95      * @param date
96      * @return
97      */
98     public static String formatDate_5(Date date) {
99         return Format_6.format(date);
100     }
101
102     /**
103      * 转换成mm月dd日 HH:mm格式的日期字符串
104      *
105      * @param date
106      * @return
107      */
108     public static String formatDate_6(Date date) {
109         return Format_7.format(date);
110     }
111
112     /**
113      * 转换成yyyy年mm月格式的日期字符串
114      *
115      * @param date
116      * @return
117      */
118     public static String formatDate_7(Date date) {
119         return Format_8.format(date);
120     }
121
122     /**
123      * 转换成mm-dd格式的日期字符串
124      *
125      * @param date
126      * @return
127      */
128     public static String formatDate_8(Date date) {
129         return Format_9.format(date);
130     }
131
132     /**
133      * 转换成yyyy年MM月dd日格式的日期字符串
134      *
135      * @param date
136      * @return
137      */
138     public static String formatDate_9(Date date) {
139         return Format_10.format(date);
140     }
141
142     /**
143      * 转换成yyyyMMddHHmm格式的日期字符串
144      *
145      * @param date
146      * @return
147      */
148     public static String formatDate_11(Date date) {
149         return Format_12.format(date);
150     }
151
152     /**
153      * 转换成yyyy年MM月dd日 HH:mm:ss格式的日期字符串
154      *
155      * @param date
156      * @return
157      */
158     public static String formatDate_10(Date date) {
159         return Format_11.format(date);
160     }
161
162     /**
163      * 转换成yyyy/MM/dd 格式的日期字符串
164      *
165      * @param date
166      * @return
167      */
168     public static String formatDate_13(Date date) {
169         return Format_13.format(date);
170     }
171
172     /**
173      * 转换成yyyy-MM 格式的日期字符串
174      *
175      * @param date
176      * @return
177      */
178     public static String formatDate_14(Date date) {
179         return Format_14.format(date);
180     }
181
182     /**
183      * 转换成yyyyMMddHHmmssSSS格式的日期字符串
184      *
185      * @param date
186      * @return
187      */
188     public static String formatDate_15(Date date) {
189         return Format_15.format(date);
190     }
191
192     /**
193      * 转换成yyyy/MM/dd HH:mm格式的日期字符串
194      *
195      * @param date
196      * @return
197      */
198     public static String formatDate_16(Date date) {
199         return Format_16.format(date);
200     }
201
202     /**
203      * 转换成HH:mm格式的日期字符串
204      *
205      * @param date
206      * @return
207      */
208     public static String formatDate_17(Date date) {
209         return Format_17.format(date);
210     }
211
212     /**
213      * 转换成字符串yyyyMMddHHmmss成日�?
214      *
215      * @param str
216      * @return
217      * @throws Exception
218      */
190d78 219     public static Date parseString(String str) {
E 220         try {
221             return Format_2.parse(str);
222         }catch (Exception e)
223         {
224             return null;
225         }
5c5945 226     }
E 227
228     /**
229      * 转换成字符串到指定的日期
230      *
231      * @param str
232      * @param format
233      * @return
234      * @throws Exception
235      */
190d78 236     public static Date parseString(String str, String format) {
5c5945 237         SimpleDateFormat sdf = new SimpleDateFormat(format);
190d78 238         try{
E 239             return sdf.parse(str);
240         }catch (Exception e)
241         {
242             return null;
243         }
5c5945 244     }
E 245
cd868c 246     /**字符串转成时间yyyy-MM-dd HH:mm:ss*/
E 247     public static Date parseString_1(String str)
248     {
249         try{
250             return  Format_3.parse(str);
251         }catch (Exception e)
252         {
253             return null;
254         }
255     }
256
257     /**字符串转成时间yyyy-MM-dd*/
258     public static Date parseString_2(String str)
259     {
260         try{
261             return Format_4.parse(str);
262         }catch (Exception e)
263         {
264             return null;
265         }
266     }
267
268
5c5945 269     /**时间上分钟叠加
E 270      * @Author: ChenJiaHe
271      * @param dateTime 时间
272      * @param min 分钟
273      * @return
274      */
275     public static Date addMin(Date dateTime,int min){
276         Calendar c = Calendar.getInstance();
277         c.setTime(dateTime);
278         c.add(Calendar.MINUTE, min);
279         return c.getTime();
280     }
281
282     /**时间上小时叠加
283      * @Author: ChenJiaHe
284      * @param dateTime 时间
285      * @param hour 小时
286      * @return
287      */
288     public static Date addhour(Date dateTime,int hour){
289         Calendar c = Calendar.getInstance();
290         c.setTime(dateTime);
291         c.add(Calendar.HOUR, hour);
292         return c.getTime();
293     }
294
295     /**时间上天数叠加
296      * @Author: ChenJiaHe
297      * @param dateTime 时间
298      * @param dayNum 天数
299      * @return
300      */
301     public static Date addDay(Date dateTime,int dayNum){
302         Calendar c = Calendar.getInstance();
303         c.setTime(dateTime);
304         c.add(Calendar.DATE, dayNum);
305         return c.getTime();
306     }
307
76b630 308     /**时间上月数叠加
C 309      * @Author: ChenJiaHe
310      * @param dateTime 时间
311      * @param dayNum 天数
312      * @return
313      */
314     public static Date addMonth(Date dateTime,int dayNum){
315         Calendar c = Calendar.getInstance();
316         c.setTime(dateTime);
317         c.add(Calendar.MONTH, dayNum);
318         return c.getTime();
319     }
320
f06126 321     /**时间上年数叠加
C 322      * @Author: ChenJiaHe
323      * @param dateTime 时间
324      * @param dayNum 天数
325      * @return
326      */
327     public static Date addYear(Date dateTime,int dayNum){
328         Calendar c = Calendar.getInstance();
329         c.setTime(dateTime);
330         c.add(Calendar.YEAR, dayNum);
331         return c.getTime();
332     }
333
5c5945 334     /**
E 335      * 转换成字符串到指定的日期
336      *
337      * @param date
338      * @param format
339      * @return
340      * @throws Exception
341      */
342     public static String formatDate(Date date, String format) {
343         if (date == null) {
344             return null;
345         }
346
347         SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
348         return sdf.format(date);
349     }
350
351     /**日期转星期
352      * @param dateTime 字符串时间
353      * @param format 字符串时间格式
354      * @param returnType 返回类型 0星期几1周几
355      * @return 数据
356      */
357     public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
358         SimpleDateFormat f = new SimpleDateFormat(format);
359         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
360         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
361             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
362         }
363         Calendar cal = Calendar.getInstance(); // 获得一个日历
364         Date datet = null;
365         datet = f.parse(dateTime);
366         cal.setTime(datet);
367
368         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
369         if (w < 0)
370             w = 0;
371         return weekDays[w];
372     }
373
374     /**日期转星期
375      * @param dateTime 时间
376      * @param returnType 返回类型 0星期几1周几
377      * @return 数据
378      */
379     public static String dateToWeek(Date dateTime,Integer returnType) {
380         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
381         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
382             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
383         }
384         Calendar cal = Calendar.getInstance(); // 获得一个日历
385         cal.setTime(dateTime);
386
387         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
388         if (w < 0)
389             w = 0;
390         return weekDays[w];
391     }
392
db7fc9 393     /**
C 394      * endTime比startTime多的天数
395      * @param startTime 最小时间
396      * @param endTime 最大时间
397      * @return
398      */
399     public static Integer differDay(Date startTime,Date endTime){
400         Calendar cal1 = Calendar.getInstance();
401         cal1.setTime(startTime);
402
403         Calendar cal2 = Calendar.getInstance();
404         cal2.setTime(endTime);
405         int day1= cal1.get(Calendar.DAY_OF_YEAR);
406         int day2 = cal2.get(Calendar.DAY_OF_YEAR);
407
408         int year1 = cal1.get(Calendar.YEAR);
409         int year2 = cal2.get(Calendar.YEAR);
410         if(year1 != year2) { //同一年
411             int timeDistance = 0 ;
412             for(int i = year1 ; i < year2 ; i ++) {
413                 if(i%4==0 && i%100!=0 || i%400==0) {//闰年
414                     timeDistance += 366;
415                 }
416                 else {//不是闰年
417                     timeDistance += 365;
418                 }
419             }
420
421             return timeDistance + (day2-day1) ;
422         }
423         else { //不同年
424             System.out.println("判断day2 - day1 : " + (day2-day1));
425             return day2-day1;
426         }
427     }
428
5c5945 429     /**判断两个时间是不是同一天*/
E 430     public static boolean timeEqual(Date startTime,Date endTime){
431        if(startTime == null || endTime==null){
432            return false;
433        }
434        boolean status = false;
435        if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
436            status = false;
437        }
438        return status;
439     }
440
63b0b0 441     /**把秒转换成X天X时X分X秒*/
E 442     public static String getChineseStr(Integer second)
443     {
444         int day = 24 * 60 * 60;
445         int hour = 60 * 60;
446         int min = 60;
447
448         int dayNum = second / day;
449         int hourNum = second % day / hour;
450         int minNum = second % day % hour / min;
451         second = second % day % hour % min;
452
453         String str = dayNum > 0 ? dayNum + "天" : "";
454         str += hourNum > 0 ? hourNum + "时" : "";
455         str += minNum > 0 ? minNum + "分" : "";
0ff9b8 456         str += second + "秒";
63b0b0 457
E 458         return str;
459     }
460
387efa 461     /**
E 462      * 针对str格式的时间做转换 格式为"xx:xx"
463      * @param time  传入的时间
464      * @return  返回分钟如果10:25,则返回625
465      */
466     public static int getMinuteNum(String time){
467
468         if(!StringUtils.isEmpty(time))
469         {
470             String[] arr = time.split(":");
471             if(arr != null && arr.length == 2)
472             {
473                 return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
474             }
475         }
476
477         return 0;
478
479
480     }
481
5c5945 482 }