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