提交 | 用户 | 时间
|
0cceb6
|
1 |
package com.jcdm.common.utils; |
Y |
2 |
|
|
3 |
import java.lang.management.ManagementFactory; |
|
4 |
import java.text.ParseException; |
|
5 |
import java.text.SimpleDateFormat; |
|
6 |
import java.time.LocalDate; |
|
7 |
import java.time.LocalDateTime; |
|
8 |
import java.time.LocalTime; |
|
9 |
import java.time.ZoneId; |
|
10 |
import java.time.ZonedDateTime; |
|
11 |
import java.util.Date; |
|
12 |
import org.apache.commons.lang3.time.DateFormatUtils; |
|
13 |
|
|
14 |
/** |
|
15 |
* 时间工具类 |
|
16 |
* |
|
17 |
* @author jc |
|
18 |
*/ |
|
19 |
public class DateUtils extends org.apache.commons.lang3.time.DateUtils |
|
20 |
{ |
|
21 |
public static String YYYY = "yyyy"; |
|
22 |
|
|
23 |
public static String YYYY_MM = "yyyy-MM"; |
|
24 |
|
|
25 |
public static String YYYY_MM_DD = "yyyy-MM-dd"; |
|
26 |
|
|
27 |
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; |
|
28 |
|
|
29 |
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; |
|
30 |
|
|
31 |
private static String[] parsePatterns = { |
|
32 |
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", |
|
33 |
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", |
|
34 |
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; |
|
35 |
|
|
36 |
/** |
|
37 |
* 获取当前Date型日期 |
|
38 |
* |
|
39 |
* @return Date() 当前日期 |
|
40 |
*/ |
|
41 |
public static Date getNowDate() |
|
42 |
{ |
|
43 |
return new Date(); |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* 获取当前日期, 默认格式为yyyy-MM-dd |
|
48 |
* |
|
49 |
* @return String |
|
50 |
*/ |
|
51 |
public static String getDate() |
|
52 |
{ |
|
53 |
return dateTimeNow(YYYY_MM_DD); |
|
54 |
} |
|
55 |
|
|
56 |
public static final String getTime() |
|
57 |
{ |
|
58 |
return dateTimeNow(YYYY_MM_DD_HH_MM_SS); |
|
59 |
} |
|
60 |
|
|
61 |
public static final String dateTimeNow() |
|
62 |
{ |
|
63 |
return dateTimeNow(YYYYMMDDHHMMSS); |
|
64 |
} |
|
65 |
|
|
66 |
public static final String dateTimeNow(final String format) |
|
67 |
{ |
|
68 |
return parseDateToStr(format, new Date()); |
|
69 |
} |
|
70 |
|
|
71 |
public static final String dateTime(final Date date) |
|
72 |
{ |
|
73 |
return parseDateToStr(YYYY_MM_DD, date); |
|
74 |
} |
|
75 |
|
|
76 |
public static final String parseDateToStr(final String format, final Date date) |
|
77 |
{ |
|
78 |
return new SimpleDateFormat(format).format(date); |
|
79 |
} |
|
80 |
|
|
81 |
public static final Date dateTime(final String format, final String ts) |
|
82 |
{ |
|
83 |
try |
|
84 |
{ |
|
85 |
return new SimpleDateFormat(format).parse(ts); |
|
86 |
} |
|
87 |
catch (ParseException e) |
|
88 |
{ |
|
89 |
throw new RuntimeException(e); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 日期路径 即年/月/日 如2018/08/08 |
|
95 |
*/ |
|
96 |
public static final String datePath() |
|
97 |
{ |
|
98 |
Date now = new Date(); |
|
99 |
return DateFormatUtils.format(now, "yyyy/MM/dd"); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 日期路径 即年/月/日 如20180808 |
|
104 |
*/ |
|
105 |
public static final String dateTime() |
|
106 |
{ |
|
107 |
Date now = new Date(); |
|
108 |
return DateFormatUtils.format(now, "yyyyMMdd"); |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* 日期型字符串转化为日期 格式 |
|
113 |
*/ |
|
114 |
public static Date parseDate(Object str) |
|
115 |
{ |
|
116 |
if (str == null) |
|
117 |
{ |
|
118 |
return null; |
|
119 |
} |
|
120 |
try |
|
121 |
{ |
|
122 |
return parseDate(str.toString(), parsePatterns); |
|
123 |
} |
|
124 |
catch (ParseException e) |
|
125 |
{ |
|
126 |
return null; |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* 获取服务器启动时间 |
|
132 |
*/ |
|
133 |
public static Date getServerStartDate() |
|
134 |
{ |
|
135 |
long time = ManagementFactory.getRuntimeMXBean().getStartTime(); |
|
136 |
return new Date(time); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 计算相差天数 |
|
141 |
*/ |
|
142 |
public static int differentDaysByMillisecond(Date date1, Date date2) |
|
143 |
{ |
|
144 |
return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* 计算时间差 |
|
149 |
* |
|
150 |
* @param endDate 最后时间 |
|
151 |
* @param startTime 开始时间 |
|
152 |
* @return 时间差(天/小时/分钟) |
|
153 |
*/ |
|
154 |
public static String timeDistance(Date endDate, Date startTime) |
|
155 |
{ |
|
156 |
long nd = 1000 * 24 * 60 * 60; |
|
157 |
long nh = 1000 * 60 * 60; |
|
158 |
long nm = 1000 * 60; |
|
159 |
// long ns = 1000; |
|
160 |
// 获得两个时间的毫秒时间差异 |
|
161 |
long diff = endDate.getTime() - startTime.getTime(); |
|
162 |
// 计算差多少天 |
|
163 |
long day = diff / nd; |
|
164 |
// 计算差多少小时 |
|
165 |
long hour = diff % nd / nh; |
|
166 |
// 计算差多少分钟 |
|
167 |
long min = diff % nd % nh / nm; |
|
168 |
// 计算差多少秒//输出结果 |
|
169 |
// long sec = diff % nd % nh % nm / ns; |
|
170 |
return day + "天" + hour + "小时" + min + "分钟"; |
|
171 |
} |
|
172 |
|
|
173 |
/** |
|
174 |
* 增加 LocalDateTime ==> Date |
|
175 |
*/ |
|
176 |
public static Date toDate(LocalDateTime temporalAccessor) |
|
177 |
{ |
|
178 |
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); |
|
179 |
return Date.from(zdt.toInstant()); |
|
180 |
} |
|
181 |
|
|
182 |
/** |
|
183 |
* 增加 LocalDate ==> Date |
|
184 |
*/ |
|
185 |
public static Date toDate(LocalDate temporalAccessor) |
|
186 |
{ |
|
187 |
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); |
|
188 |
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); |
|
189 |
return Date.from(zdt.toInstant()); |
|
190 |
} |
|
191 |
} |