提交 | 用户 | 时间
|
71e81e
|
1 |
package cn.stylefeng.guns.plcserver.tool; |
懒 |
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.GregorianCalendar; |
|
8 |
|
|
9 |
public class DateTool { |
|
10 |
|
|
11 |
public static String getLocalTime() { |
|
12 |
Date date = new Date(); |
|
13 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
|
14 |
String localTime = formatter.format(date); |
|
15 |
return localTime; |
|
16 |
} |
|
17 |
|
|
18 |
public static Date getLocalTimeForDate() { |
|
19 |
//创建SimpleDateFormat对象实例并定义好转换格式 |
|
20 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
21 |
|
|
22 |
Date date = new Date(); |
|
23 |
try { |
|
24 |
String localTime = sdf.format(date); |
|
25 |
// 注意格式需要与上面一致,不然会出现异常 |
|
26 |
date = sdf.parse(localTime); |
|
27 |
} catch (ParseException e) { |
|
28 |
e.printStackTrace(); |
|
29 |
} |
|
30 |
return date; |
|
31 |
} |
|
32 |
|
|
33 |
public static Calendar getCalendar(Date date) { |
|
34 |
Calendar cal = Calendar.getInstance(); |
|
35 |
cal.setTime(date); |
|
36 |
return cal; |
|
37 |
} |
|
38 |
|
|
39 |
public static String getCurrentDate(String pattern) { |
|
40 |
Date date = new Date(); |
|
41 |
|
|
42 |
String s = dateToStr(date, pattern); |
|
43 |
return s; |
|
44 |
} |
|
45 |
|
|
46 |
public static Date parseDate(String source) { |
|
47 |
if ((source == null) || (source.length() == 0)) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
String[] datePattern = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss" }; |
|
51 |
Date date = null; |
|
52 |
for (int i = 0; i < datePattern.length; i++) { |
|
53 |
date = parseDate(source, datePattern[i]); |
|
54 |
if (date != null) { |
|
55 |
break; |
|
56 |
} |
|
57 |
} |
|
58 |
return date; |
|
59 |
} |
|
60 |
|
|
61 |
public static Calendar parseCal(String source) { |
|
62 |
if ((source == null) || (source.length() == 0)) { |
|
63 |
return null; |
|
64 |
} |
|
65 |
String[] datePattern = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss" }; |
|
66 |
Calendar date = null; |
|
67 |
for (int i = 0; i < datePattern.length; i++) { |
|
68 |
date = parseCal(source, datePattern[i]); |
|
69 |
if (date != null) { |
|
70 |
break; |
|
71 |
} |
|
72 |
} |
|
73 |
return date; |
|
74 |
} |
|
75 |
|
|
76 |
public static Date parseDate(String dateStr, String format) { |
|
77 |
SimpleDateFormat dateFormat = new SimpleDateFormat(format); |
|
78 |
Date date = null; |
|
79 |
try { |
|
80 |
date = dateFormat.parse(dateStr); |
|
81 |
} catch (ParseException localParseException) { |
|
82 |
} |
|
83 |
return date; |
|
84 |
} |
|
85 |
|
|
86 |
public static Calendar parseCal(String dateStr, String format) { |
|
87 |
SimpleDateFormat dateFormat = new SimpleDateFormat(format); |
|
88 |
Calendar date = Calendar.getInstance(); |
|
89 |
try { |
|
90 |
date.setTime(dateFormat.parse(dateStr)); |
|
91 |
} catch (ParseException localParseException) { |
|
92 |
} |
|
93 |
return date; |
|
94 |
} |
|
95 |
|
|
96 |
public static int diffDate(Date end, Date start) { |
|
97 |
return (int) (getMillis(end) / 86400000L - getMillis(start) / 86400000L); |
|
98 |
} |
|
99 |
|
|
100 |
public static long diffSecond(Date end, Date start) { |
|
101 |
return (getMillis(end) - getMillis(start)) / 1000L; |
|
102 |
} |
|
103 |
|
|
104 |
private static long getMillis(Date date) { |
|
105 |
Calendar c = Calendar.getInstance(); |
|
106 |
c.setTime(date); |
|
107 |
return c.getTimeInMillis(); |
|
108 |
} |
|
109 |
|
|
110 |
public static String getNow() { |
|
111 |
Date now = new Date(); |
|
112 |
return formatDateTime(now); |
|
113 |
} |
|
114 |
|
|
115 |
public static String getNow(String pattern) { |
|
116 |
Date now = new Date(); |
|
117 |
return formatDate(now, pattern); |
|
118 |
} |
|
119 |
|
|
120 |
public static String formatDate(String dateStr, String pattern) { |
|
121 |
Date date = parseDate(dateStr); |
|
122 |
return formatDate(date, pattern); |
|
123 |
} |
|
124 |
|
|
125 |
public static String formatTime(Date date) { |
|
126 |
String pattern = "HH:mm:ss"; |
|
127 |
return formatDate(date, pattern); |
|
128 |
} |
|
129 |
|
|
130 |
public static String formatDate(Date date) { |
|
131 |
String pattern = "yyyy-MM-dd"; |
|
132 |
return formatDate(date, pattern); |
|
133 |
} |
|
134 |
|
|
135 |
public static String formatDateTime(Date date) { |
|
136 |
String pattern = "yyyy-MM-dd HH:mm:ss"; |
|
137 |
return formatDate(date, pattern); |
|
138 |
} |
|
139 |
|
|
140 |
public static String formatDateTime(Calendar cal) { |
|
141 |
String pattern = "yyyy-MM-dd HH:mm:ss"; |
|
142 |
if (cal == null) { |
|
143 |
return ""; |
|
144 |
} |
|
145 |
return formatDate(cal.getTime(), pattern); |
|
146 |
} |
|
147 |
|
|
148 |
public static String formatDate(Date date, String pattern) { |
|
149 |
String dateTimePattern = pattern; |
|
150 |
if (date == null) { |
|
151 |
if ((dateTimePattern.length() > 2) |
|
152 |
&& (dateTimePattern.substring(dateTimePattern.length() - 2).equals("_e"))) { |
|
153 |
dateTimePattern = dateTimePattern.replaceAll("(yy|YY){1,2}", " "); |
|
154 |
dateTimePattern = dateTimePattern.replaceAll("(M){1,2}", " "); |
|
155 |
dateTimePattern = dateTimePattern.replaceAll("(d|D){1,2}", " "); |
|
156 |
dateTimePattern = dateTimePattern.replaceAll("(h|H){1,2}", " "); |
|
157 |
dateTimePattern = dateTimePattern.replaceAll("(m){1,2}", " "); |
|
158 |
dateTimePattern = dateTimePattern.replaceAll("(s|S){1,2}", " "); |
|
159 |
|
|
160 |
return dateTimePattern.substring(0, dateTimePattern.length() - 2); |
|
161 |
} |
|
162 |
return ""; |
|
163 |
} |
|
164 |
if ((dateTimePattern.length() > 2) && (dateTimePattern.substring(dateTimePattern.length() - 2).equals("_e"))) { |
|
165 |
dateTimePattern = dateTimePattern.substring(0, dateTimePattern.length() - 2); |
|
166 |
} |
|
167 |
if ((dateTimePattern == null) || (dateTimePattern.length() == 0)) { |
|
168 |
return formatDateTime(date); |
|
169 |
} |
|
170 |
SimpleDateFormat sdf = new SimpleDateFormat(); |
|
171 |
try { |
|
172 |
sdf.applyPattern(dateTimePattern); |
|
173 |
} catch (IllegalArgumentException exp) { |
|
174 |
sdf.applyPattern("yyyy-MM-dd HH:mm:ss"); |
|
175 |
} |
|
176 |
return sdf.format(date); |
|
177 |
} |
|
178 |
|
|
179 |
public static int getCurrYear() { |
|
180 |
Calendar cal = Calendar.getInstance(); |
|
181 |
return cal.get(1); |
|
182 |
} |
|
183 |
|
|
184 |
public static int getCurrMonth() { |
|
185 |
Calendar cal = Calendar.getInstance(); |
|
186 |
return cal.get(2) + 1; |
|
187 |
} |
|
188 |
|
|
189 |
public static String getCurrtWeekDay() { |
|
190 |
Date date = new Date(); |
|
191 |
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; |
|
192 |
Calendar cal = Calendar.getInstance(); |
|
193 |
cal.setTime(date); |
|
194 |
int w = cal.get(7) - 1; |
|
195 |
if (w < 0) { |
|
196 |
w = 0; |
|
197 |
} |
|
198 |
return weekDays[w]; |
|
199 |
} |
|
200 |
|
|
201 |
public static int getCurrDay() { |
|
202 |
Calendar cal = Calendar.getInstance(); |
|
203 |
return cal.get(5); |
|
204 |
} |
|
205 |
|
|
206 |
public static int getWeekOfYear(Date date) { |
|
207 |
Calendar c = new GregorianCalendar(); |
|
208 |
c.setFirstDayOfWeek(2); |
|
209 |
c.setMinimalDaysInFirstWeek(7); |
|
210 |
c.setTime(date); |
|
211 |
|
|
212 |
return c.get(3); |
|
213 |
} |
|
214 |
|
|
215 |
public static int getWeekOfYear() { |
|
216 |
return getWeekOfYear(new Date()); |
|
217 |
} |
|
218 |
|
|
219 |
public static int getMaxWeekNumOfYear(int year) { |
|
220 |
Calendar c = new GregorianCalendar(); |
|
221 |
c.set(year, 11, 31, 23, 59, 59); |
|
222 |
|
|
223 |
return getWeekOfYear(c.getTime()); |
|
224 |
} |
|
225 |
|
|
226 |
public static Date getFirstDayOfWeek(int year, int week) { |
|
227 |
Calendar c = new GregorianCalendar(); |
|
228 |
c.set(1, year); |
|
229 |
c.set(2, 0); |
|
230 |
c.set(5, 1); |
|
231 |
|
|
232 |
Calendar cal = (GregorianCalendar) c.clone(); |
|
233 |
cal.add(5, week * 7); |
|
234 |
|
|
235 |
return getFirstDayOfWeek(cal.getTime()); |
|
236 |
} |
|
237 |
|
|
238 |
public static Date getLastDayOfWeek(int year, int week) { |
|
239 |
Calendar c = new GregorianCalendar(); |
|
240 |
c.set(1, year); |
|
241 |
c.set(2, 0); |
|
242 |
c.set(5, 1); |
|
243 |
|
|
244 |
Calendar cal = (GregorianCalendar) c.clone(); |
|
245 |
cal.add(5, week * 7); |
|
246 |
|
|
247 |
return getLastDayOfWeek(cal.getTime()); |
|
248 |
} |
|
249 |
|
|
250 |
public static Date getFirstDayOfWeek(Date date) { |
|
251 |
Calendar c = new GregorianCalendar(); |
|
252 |
c.setFirstDayOfWeek(2); |
|
253 |
c.setTime(date); |
|
254 |
c.set(7, c.getFirstDayOfWeek()); |
|
255 |
return c.getTime(); |
|
256 |
} |
|
257 |
|
|
258 |
public static Date getLastDayOfWeek(Date date) { |
|
259 |
Calendar c = new GregorianCalendar(); |
|
260 |
c.setFirstDayOfWeek(2); |
|
261 |
c.setTime(date); |
|
262 |
c.set(7, c.getFirstDayOfWeek() + 6); |
|
263 |
return c.getTime(); |
|
264 |
} |
|
265 |
|
|
266 |
public static Date getFirstDayOfFirstWeek(int year, int month) { |
|
267 |
Calendar c = Calendar.getInstance(); |
|
268 |
c.setFirstDayOfWeek(2); |
|
269 |
c.set(year, month, 1); |
|
270 |
|
|
271 |
c.set(8, 1); |
|
272 |
c.set(7, 2); |
|
273 |
|
|
274 |
return c.getTime(); |
|
275 |
} |
|
276 |
|
|
277 |
public static Date getYesterday() { |
|
278 |
Calendar c = new GregorianCalendar(); |
|
279 |
c.set(5, c.get(5) - 1); |
|
280 |
Date date = c.getTime(); |
|
281 |
return date; |
|
282 |
} |
|
283 |
|
|
284 |
public static Date getTomorrow() { |
|
285 |
Calendar c = new GregorianCalendar(); |
|
286 |
c.set(5, c.get(5) + 1); |
|
287 |
Date date = c.getTime(); |
|
288 |
return date; |
|
289 |
} |
|
290 |
|
|
291 |
public static Date getPreMonth(Date date) { |
|
292 |
GregorianCalendar c = new GregorianCalendar(); |
|
293 |
c.setTime(date); |
|
294 |
c.add(2, -1); |
|
295 |
return c.getTime(); |
|
296 |
} |
|
297 |
|
|
298 |
|
|
299 |
public static int getDayCountByTwoDates(String strDate1, String strDate2) { |
|
300 |
int differ = 0; |
|
301 |
try { |
|
302 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
|
303 |
Date date1 = format.parse(strDate1); |
|
304 |
Date date2 = format.parse(strDate2); |
|
305 |
differ = (int) ((date1.getTime() - date2.getTime()) / 86400000L); |
|
306 |
} catch (Exception e) { |
|
307 |
differ = -1; |
|
308 |
} |
|
309 |
return differ; |
|
310 |
} |
|
311 |
|
|
312 |
public static String dateToStr(Date date, String pattern) { |
|
313 |
if (date == null) { |
|
314 |
return ""; |
|
315 |
} |
|
316 |
String pat = pattern; |
|
317 |
if ((pat == null) || (pat.length() == 0)) { |
|
318 |
pat = "yyyy-MM-dd HH:mm:ss"; |
|
319 |
} |
|
320 |
SimpleDateFormat sdf = new SimpleDateFormat(pat); |
|
321 |
|
|
322 |
return sdf.format(date); |
|
323 |
} |
|
324 |
|
|
325 |
public static Calendar addSeconds(Calendar cal, int amount) { |
|
326 |
Calendar cal2 = Calendar.getInstance(); |
|
327 |
cal2.setTime(cal.getTime()); |
|
328 |
cal2.add(13, amount); |
|
329 |
return cal2; |
|
330 |
} |
|
331 |
} |