chenjiahe
2021-11-12 f2a234279e95184c0b79f4512a5e15e55dddd1f7
提交 | 用户 | 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
205     /**
206      * 转换成yyyyMMddHHmmssSSS格式的日期字符串
207      *
208      * @param date
209      * @return
210      */
211     public static String formatDate_15(Date date) {
212         return Format_15.format(date);
213     }
214
215     /**
216      * 转换成yyyy/MM/dd HH:mm格式的日期字符串
217      *
218      * @param date
219      * @return
220      */
221     public static String formatDate_16(Date date) {
222         return Format_16.format(date);
223     }
224
225     /**
226      * 转换成HH:mm格式的日期字符串
227      *
228      * @param date
229      * @return
230      */
231     public static String formatDate_17(Date date) {
232         return Format_17.format(date);
233     }
234
235     /**
236      * 转换成字符串yyyyMMddHHmmss成日�?
237      *
238      * @param str
239      * @return
240      * @throws Exception
241      */
190d78 242     public static Date parseString(String str) {
E 243         try {
244             return Format_2.parse(str);
245         }catch (Exception e)
246         {
247             return null;
248         }
5c5945 249     }
E 250
251     /**
252      * 转换成字符串到指定的日期
253      *
254      * @param str
255      * @param format
256      * @return
257      * @throws Exception
258      */
190d78 259     public static Date parseString(String str, String format) {
5c5945 260         SimpleDateFormat sdf = new SimpleDateFormat(format);
190d78 261         try{
E 262             return sdf.parse(str);
263         }catch (Exception e)
264         {
265             return null;
266         }
5c5945 267     }
E 268
cd868c 269     /**字符串转成时间yyyy-MM-dd HH:mm:ss*/
E 270     public static Date parseString_1(String str)
271     {
272         try{
273             return  Format_3.parse(str);
274         }catch (Exception e)
275         {
276             return null;
277         }
278     }
279
280     /**字符串转成时间yyyy-MM-dd*/
281     public static Date parseString_2(String str)
282     {
283         try{
284             return Format_4.parse(str);
285         }catch (Exception e)
286         {
287             return null;
288         }
289     }
290
291
5c5945 292     /**时间上分钟叠加
E 293      * @Author: ChenJiaHe
294      * @param dateTime 时间
295      * @param min 分钟
296      * @return
297      */
298     public static Date addMin(Date dateTime,int min){
299         Calendar c = Calendar.getInstance();
300         c.setTime(dateTime);
301         c.add(Calendar.MINUTE, min);
302         return c.getTime();
303     }
304
305     /**时间上小时叠加
306      * @Author: ChenJiaHe
307      * @param dateTime 时间
308      * @param hour 小时
309      * @return
310      */
311     public static Date addhour(Date dateTime,int hour){
312         Calendar c = Calendar.getInstance();
313         c.setTime(dateTime);
314         c.add(Calendar.HOUR, hour);
315         return c.getTime();
316     }
317
318     /**时间上天数叠加
319      * @Author: ChenJiaHe
320      * @param dateTime 时间
321      * @param dayNum 天数
322      * @return
323      */
324     public static Date addDay(Date dateTime,int dayNum){
325         Calendar c = Calendar.getInstance();
326         c.setTime(dateTime);
327         c.add(Calendar.DATE, dayNum);
328         return c.getTime();
329     }
330
76b630 331     /**时间上月数叠加
C 332      * @Author: ChenJiaHe
333      * @param dateTime 时间
334      * @param dayNum 天数
335      * @return
336      */
337     public static Date addMonth(Date dateTime,int dayNum){
338         Calendar c = Calendar.getInstance();
339         c.setTime(dateTime);
340         c.add(Calendar.MONTH, dayNum);
341         return c.getTime();
342     }
343
f06126 344     /**时间上年数叠加
C 345      * @Author: ChenJiaHe
346      * @param dateTime 时间
347      * @param dayNum 天数
348      * @return
349      */
350     public static Date addYear(Date dateTime,int dayNum){
351         Calendar c = Calendar.getInstance();
352         c.setTime(dateTime);
353         c.add(Calendar.YEAR, dayNum);
354         return c.getTime();
355     }
356
5c5945 357     /**
E 358      * 转换成字符串到指定的日期
359      *
360      * @param date
361      * @param format
362      * @return
363      * @throws Exception
364      */
365     public static String formatDate(Date date, String format) {
366         if (date == null) {
367             return null;
368         }
369
370         SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
371         return sdf.format(date);
372     }
373
374     /**日期转星期
375      * @param dateTime 字符串时间
376      * @param format 字符串时间格式
377      * @param returnType 返回类型 0星期几1周几
378      * @return 数据
379      */
380     public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
381         SimpleDateFormat f = new SimpleDateFormat(format);
382         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
383         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
384             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
385         }
386         Calendar cal = Calendar.getInstance(); // 获得一个日历
387         Date datet = null;
388         datet = f.parse(dateTime);
389         cal.setTime(datet);
390
391         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
392         if (w < 0)
393             w = 0;
394         return weekDays[w];
395     }
396
397     /**日期转星期
398      * @param dateTime 时间
399      * @param returnType 返回类型 0星期几1周几
400      * @return 数据
401      */
402     public static String dateToWeek(Date dateTime,Integer returnType) {
403         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
404         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
405             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
406         }
407         Calendar cal = Calendar.getInstance(); // 获得一个日历
408         cal.setTime(dateTime);
409
410         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
411         if (w < 0)
412             w = 0;
413         return weekDays[w];
414     }
415
db7fc9 416     /**
C 417      * endTime比startTime多的天数
418      * @param startTime 最小时间
419      * @param endTime 最大时间
420      * @return
421      */
422     public static Integer differDay(Date startTime,Date endTime){
423         Calendar cal1 = Calendar.getInstance();
424         cal1.setTime(startTime);
425
426         Calendar cal2 = Calendar.getInstance();
427         cal2.setTime(endTime);
428         int day1= cal1.get(Calendar.DAY_OF_YEAR);
429         int day2 = cal2.get(Calendar.DAY_OF_YEAR);
430
431         int year1 = cal1.get(Calendar.YEAR);
432         int year2 = cal2.get(Calendar.YEAR);
433         if(year1 != year2) { //同一年
434             int timeDistance = 0 ;
435             for(int i = year1 ; i < year2 ; i ++) {
436                 if(i%4==0 && i%100!=0 || i%400==0) {//闰年
437                     timeDistance += 366;
438                 }
439                 else {//不是闰年
440                     timeDistance += 365;
441                 }
442             }
443
444             return timeDistance + (day2-day1) ;
445         }
446         else { //不同年
447             System.out.println("判断day2 - day1 : " + (day2-day1));
448             return day2-day1;
449         }
450     }
451
5c5945 452     /**判断两个时间是不是同一天*/
E 453     public static boolean timeEqual(Date startTime,Date endTime){
454        if(startTime == null || endTime==null){
455            return false;
456        }
457        boolean status = false;
458        if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
459            status = false;
460        }
461        return status;
462     }
463
63b0b0 464     /**把秒转换成X天X时X分X秒*/
E 465     public static String getChineseStr(Integer second)
466     {
467         int day = 24 * 60 * 60;
468         int hour = 60 * 60;
469         int min = 60;
470
471         int dayNum = second / day;
472         int hourNum = second % day / hour;
473         int minNum = second % day % hour / min;
474         second = second % day % hour % min;
475
476         String str = dayNum > 0 ? dayNum + "天" : "";
477         str += hourNum > 0 ? hourNum + "时" : "";
478         str += minNum > 0 ? minNum + "分" : "";
0ff9b8 479         str += second + "秒";
63b0b0 480
E 481         return str;
482     }
483
387efa 484     /**
E 485      * 针对str格式的时间做转换 格式为"xx:xx"
486      * @param time  传入的时间
487      * @return  返回分钟如果10:25,则返回625
488      */
489     public static int getMinuteNum(String time){
490
491         if(!StringUtils.isEmpty(time))
492         {
493             String[] arr = time.split(":");
494             if(arr != null && arr.length == 2)
495             {
496                 return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
497             }
498         }
499
500         return 0;
501
502
503     }
504
5c5945 505 }