ChenJiaHe
2021-02-24 cac339cb773b12721bc2c9886d5ab7ed9753f245
提交 | 用户 | 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
f06126 308     /**时间上年数叠加
C 309      * @Author: ChenJiaHe
310      * @param dateTime 时间
311      * @param dayNum 天数
312      * @return
313      */
314     public static Date addYear(Date dateTime,int dayNum){
315         Calendar c = Calendar.getInstance();
316         c.setTime(dateTime);
317         c.add(Calendar.YEAR, dayNum);
318         return c.getTime();
319     }
320
5c5945 321     /**
E 322      * 转换成字符串到指定的日期
323      *
324      * @param date
325      * @param format
326      * @return
327      * @throws Exception
328      */
329     public static String formatDate(Date date, String format) {
330         if (date == null) {
331             return null;
332         }
333
334         SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
335         return sdf.format(date);
336     }
337
338     /**日期转星期
339      * @param dateTime 字符串时间
340      * @param format 字符串时间格式
341      * @param returnType 返回类型 0星期几1周几
342      * @return 数据
343      */
344     public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
345         SimpleDateFormat f = new SimpleDateFormat(format);
346         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
347         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
348             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
349         }
350         Calendar cal = Calendar.getInstance(); // 获得一个日历
351         Date datet = null;
352         datet = f.parse(dateTime);
353         cal.setTime(datet);
354
355         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
356         if (w < 0)
357             w = 0;
358         return weekDays[w];
359     }
360
361     /**日期转星期
362      * @param dateTime 时间
363      * @param returnType 返回类型 0星期几1周几
364      * @return 数据
365      */
366     public static String dateToWeek(Date dateTime,Integer returnType) {
367         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
368         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
369             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
370         }
371         Calendar cal = Calendar.getInstance(); // 获得一个日历
372         cal.setTime(dateTime);
373
374         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
375         if (w < 0)
376             w = 0;
377         return weekDays[w];
378     }
379
db7fc9 380     /**
C 381      * endTime比startTime多的天数
382      * @param startTime 最小时间
383      * @param endTime 最大时间
384      * @return
385      */
386     public static Integer differDay(Date startTime,Date endTime){
387         Calendar cal1 = Calendar.getInstance();
388         cal1.setTime(startTime);
389
390         Calendar cal2 = Calendar.getInstance();
391         cal2.setTime(endTime);
392         int day1= cal1.get(Calendar.DAY_OF_YEAR);
393         int day2 = cal2.get(Calendar.DAY_OF_YEAR);
394
395         int year1 = cal1.get(Calendar.YEAR);
396         int year2 = cal2.get(Calendar.YEAR);
397         if(year1 != year2) { //同一年
398             int timeDistance = 0 ;
399             for(int i = year1 ; i < year2 ; i ++) {
400                 if(i%4==0 && i%100!=0 || i%400==0) {//闰年
401                     timeDistance += 366;
402                 }
403                 else {//不是闰年
404                     timeDistance += 365;
405                 }
406             }
407
408             return timeDistance + (day2-day1) ;
409         }
410         else { //不同年
411             System.out.println("判断day2 - day1 : " + (day2-day1));
412             return day2-day1;
413         }
414     }
415
5c5945 416     /**判断两个时间是不是同一天*/
E 417     public static boolean timeEqual(Date startTime,Date endTime){
418        if(startTime == null || endTime==null){
419            return false;
420        }
421        boolean status = false;
422        if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
423            status = false;
424        }
425        return status;
426     }
427
63b0b0 428     /**把秒转换成X天X时X分X秒*/
E 429     public static String getChineseStr(Integer second)
430     {
431         int day = 24 * 60 * 60;
432         int hour = 60 * 60;
433         int min = 60;
434
435         int dayNum = second / day;
436         int hourNum = second % day / hour;
437         int minNum = second % day % hour / min;
438         second = second % day % hour % min;
439
440         String str = dayNum > 0 ? dayNum + "天" : "";
441         str += hourNum > 0 ? hourNum + "时" : "";
442         str += minNum > 0 ? minNum + "分" : "";
0ff9b8 443         str += second + "秒";
63b0b0 444
E 445         return str;
446     }
447
387efa 448     /**
E 449      * 针对str格式的时间做转换 格式为"xx:xx"
450      * @param time  传入的时间
451      * @return  返回分钟如果10:25,则返回625
452      */
453     public static int getMinuteNum(String time){
454
455         if(!StringUtils.isEmpty(time))
456         {
457             String[] arr = time.split(":");
458             if(arr != null && arr.length == 2)
459             {
460                 return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
461             }
462         }
463
464         return 0;
465
466
467     }
468
5c5945 469 }