E1ED922C1E9526DD63272D7EC5C6CB77
2020-11-04 0ff9b899a773dbaa93dfbd16d37925398899f57d
提交 | 用户 | 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
236     /**时间上分钟叠加
237      * @Author: ChenJiaHe
238      * @param dateTime 时间
239      * @param min 分钟
240      * @return
241      */
242     public static Date addMin(Date dateTime,int min){
243         Calendar c = Calendar.getInstance();
244         c.setTime(dateTime);
245         c.add(Calendar.MINUTE, min);
246         return c.getTime();
247     }
248
249     /**时间上小时叠加
250      * @Author: ChenJiaHe
251      * @param dateTime 时间
252      * @param hour 小时
253      * @return
254      */
255     public static Date addhour(Date dateTime,int hour){
256         Calendar c = Calendar.getInstance();
257         c.setTime(dateTime);
258         c.add(Calendar.HOUR, hour);
259         return c.getTime();
260     }
261
262     /**时间上天数叠加
263      * @Author: ChenJiaHe
264      * @param dateTime 时间
265      * @param dayNum 天数
266      * @return
267      */
268     public static Date addDay(Date dateTime,int dayNum){
269         Calendar c = Calendar.getInstance();
270         c.setTime(dateTime);
271         c.add(Calendar.DATE, dayNum);
272         return c.getTime();
273     }
274
275     /**
276      * 转换成字符串到指定的日期
277      *
278      * @param date
279      * @param format
280      * @return
281      * @throws Exception
282      */
283     public static String formatDate(Date date, String format) {
284         if (date == null) {
285             return null;
286         }
287
288         SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
289         return sdf.format(date);
290     }
291
292     /**日期转星期
293      * @param dateTime 字符串时间
294      * @param format 字符串时间格式
295      * @param returnType 返回类型 0星期几1周几
296      * @return 数据
297      */
298     public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
299         SimpleDateFormat f = new SimpleDateFormat(format);
300         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
301         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
302             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
303         }
304         Calendar cal = Calendar.getInstance(); // 获得一个日历
305         Date datet = null;
306         datet = f.parse(dateTime);
307         cal.setTime(datet);
308
309         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
310         if (w < 0)
311             w = 0;
312         return weekDays[w];
313     }
314
315     /**日期转星期
316      * @param dateTime 时间
317      * @param returnType 返回类型 0星期几1周几
318      * @return 数据
319      */
320     public static String dateToWeek(Date dateTime,Integer returnType) {
321         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
322         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
323             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
324         }
325         Calendar cal = Calendar.getInstance(); // 获得一个日历
326         cal.setTime(dateTime);
327
328         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
329         if (w < 0)
330             w = 0;
331         return weekDays[w];
332     }
333
334     /**判断两个时间是不是同一天*/
335     public static boolean timeEqual(Date startTime,Date endTime){
336        if(startTime == null || endTime==null){
337            return false;
338        }
339        boolean status = false;
340        if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
341            status = false;
342        }
343        return status;
344     }
345
63b0b0 346     /**把秒转换成X天X时X分X秒*/
E 347     public static String getChineseStr(Integer second)
348     {
349         int day = 24 * 60 * 60;
350         int hour = 60 * 60;
351         int min = 60;
352
353         int dayNum = second / day;
354         int hourNum = second % day / hour;
355         int minNum = second % day % hour / min;
356         second = second % day % hour % min;
357
358         String str = dayNum > 0 ? dayNum + "天" : "";
359         str += hourNum > 0 ? hourNum + "时" : "";
360         str += minNum > 0 ? minNum + "分" : "";
0ff9b8 361         str += second + "秒";
63b0b0 362
E 363         return str;
364     }
365
5c5945 366 }