E1ED922C1E9526DD63272D7EC5C6CB77
2020-11-25 cd868cf9e1172167bcb2aaa1595191a5cf08083f
提交 | 用户 | 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
298     /**
299      * 转换成字符串到指定的日期
300      *
301      * @param date
302      * @param format
303      * @return
304      * @throws Exception
305      */
306     public static String formatDate(Date date, String format) {
307         if (date == null) {
308             return null;
309         }
310
311         SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
312         return sdf.format(date);
313     }
314
315     /**日期转星期
316      * @param dateTime 字符串时间
317      * @param format 字符串时间格式
318      * @param returnType 返回类型 0星期几1周几
319      * @return 数据
320      */
321     public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
322         SimpleDateFormat f = new SimpleDateFormat(format);
323         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
324         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
325             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
326         }
327         Calendar cal = Calendar.getInstance(); // 获得一个日历
328         Date datet = null;
329         datet = f.parse(dateTime);
330         cal.setTime(datet);
331
332         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
333         if (w < 0)
334             w = 0;
335         return weekDays[w];
336     }
337
338     /**日期转星期
339      * @param dateTime 时间
340      * @param returnType 返回类型 0星期几1周几
341      * @return 数据
342      */
343     public static String dateToWeek(Date dateTime,Integer returnType) {
344         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
345         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
346             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
347         }
348         Calendar cal = Calendar.getInstance(); // 获得一个日历
349         cal.setTime(dateTime);
350
351         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
352         if (w < 0)
353             w = 0;
354         return weekDays[w];
355     }
356
357     /**判断两个时间是不是同一天*/
358     public static boolean timeEqual(Date startTime,Date endTime){
359        if(startTime == null || endTime==null){
360            return false;
361        }
362        boolean status = false;
363        if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
364            status = false;
365        }
366        return status;
367     }
368
63b0b0 369     /**把秒转换成X天X时X分X秒*/
E 370     public static String getChineseStr(Integer second)
371     {
372         int day = 24 * 60 * 60;
373         int hour = 60 * 60;
374         int min = 60;
375
376         int dayNum = second / day;
377         int hourNum = second % day / hour;
378         int minNum = second % day % hour / min;
379         second = second % day % hour % min;
380
381         String str = dayNum > 0 ? dayNum + "天" : "";
382         str += hourNum > 0 ? hourNum + "时" : "";
383         str += minNum > 0 ? minNum + "分" : "";
0ff9b8 384         str += second + "秒";
63b0b0 385
E 386         return str;
387     }
388
5c5945 389 }