-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateUtil.java
461 lines (413 loc) · 11.5 KB
/
DateUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.gootrip.util;
/**
* <p>Title: 时间和日期的工具类</p>
* <p>Description: DateUtil类包含了标准的时间和日期格式,以及这些格式在字符串及日期之间转换的方法</p>
* <p>Copyright: Copyright (c) 2007 advance,Inc. All Rights Reserved</p>
* <p>Company: advance,Inc.</p>
* @author advance
* @version 1.0
*/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
//~ Static fields/initializers =============================================
private static String datePattern = "MM/dd/yyyy";
private static String timePattern = datePattern + " HH:MM a";
//~ Methods ================================================================
/**
* Return default datePattern (MM/dd/yyyy)
* @return a string representing the date pattern on the UI
*/
public static String getDatePattern() {
return datePattern;
}
/**
* This method attempts to convert an Oracle-formatted date
* in the form dd-MMM-yyyy to mm/dd/yyyy.
*
* @param aDate date from database as a string
* @return formatted string for the ui
*/
public static final String getDate(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(datePattern);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String date2Str(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(datePattern);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String date2Str(String pattern, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(pattern);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* This method generates a string representation of a date/time
* in the format you specify on input
*
* @param aMask the date pattern the string is in
* @param strDate a string representation of a date
* @return a converted Date object
* @see java.text.SimpleDateFormat
* @throws ParseException
*/
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
try {
date = df.parse(strDate);
} catch (ParseException pe) {
return null;
}
return (date);
}
public static final Date str2Date(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
try {
date = df.parse(strDate);
} catch (ParseException pe) {
return null;
}
return (date);
}
/**
* This method returns the current date time in the format:
* MM/dd/yyyy HH:MM a
*
* @param theTime the current time
* @return the current date/time
*/
public static String getTimeNow(Date theTime) {
return getDateTime(timePattern, theTime);
}
/**
* This method returns the current date in the format: MM/dd/yyyy
*
* @return the current date
* @throws ParseException
*/
public static Calendar getToday() throws ParseException {
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(datePattern);
// This seems like quite a hack (date -> string -> date),
// but it works ;-)
String todayAsString = df.format(today);
Calendar cal = new GregorianCalendar();
cal.setTime(convertStringToDate(todayAsString));
return cal;
}
/**
* This method generates a string representation of a date's date/time
* in the format you specify on input
*
* @param aMask the date pattern the string is in
* @param aDate a date object
* @return a formatted string representation of the date
*
* @see java.text.SimpleDateFormat
*/
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
System.out.print("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* This method generates a string representation of a date based
* on the System Property 'dateFormat'
* in the format you specify on input
*
* @param aDate A date to convert
* @return a string representation of the date
*/
public static final String convertDateToString(Date aDate) {
return getDateTime(datePattern, aDate);
}
/**
* This method converts a String to a date using the datePattern
*
* @param strDate the date to convert (in format MM/dd/yyyy)
* @return a date object
*
* @throws ParseException
*/
public static Date convertStringToDate(String strDate)
throws ParseException {
Date aDate = null;
try {
aDate = convertStringToDate(datePattern, strDate);
} catch (ParseException pe) {
//log.error("Could not convert '" + strDate
// + "' to a date, throwing exception");
pe.printStackTrace();
return null;
}
return aDate;
}
//日期格式转换成时间戳
public static long getTimeStamp(String pattern, String strDate) {
long returnTimeStamp = 0;
Date aDate = null;
try {
aDate = convertStringToDate(pattern, strDate);
} catch (ParseException pe) {
aDate = null;
}
if (aDate == null) {
returnTimeStamp = 0;
} else {
returnTimeStamp = aDate.getTime();
}
return returnTimeStamp;
}
//获取当前日期的邮戳
public static long getNowTimeStamp() {
long returnTimeStamp = 0;
Date aDate = null;
try {
aDate = convertStringToDate("yyyy-MM-dd HH:mm:ss", getNowDateTime());
} catch (ParseException pe) {
aDate = null;
}
if (aDate == null) {
returnTimeStamp = 0;
} else {
returnTimeStamp = aDate.getTime();
}
return returnTimeStamp;
}
/**
*得到格式化后的系统当前日期
*@param strScheme 格式模式字符串
*@return 格式化后的系统当前时间,如果有异常产生,返回空串""
*@see java.util.SimpleDateFormat
*/
public static final String getNowDateTime(String strScheme) {
String strReturn = null;
Date now = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat(strScheme);
strReturn = sdf.format(now);
} catch (Exception e) {
strReturn = "";
}
return strReturn;
}
public static final String getNowDateTime() {
String strReturn = null;
Date now = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
strReturn = sdf.format(now);
} catch (Exception e) {
strReturn = "";
}
return strReturn;
}
/**
*转化日期格式"MM/dd/YY、MM.dd.YY、MM-dd-YY、MM/dd/YY",并输出为正常的格式yyyy-MM-dd
*@param strDate 待转换的日期格式
*@return 格式化后的日期,如果有异常产生,返回空串""
*@see java.util.SimpleDateFormat
*/
public static final String convertNormalDate(String strDate) {
String strReturn = null;
//先把传入参数分格符转换成java认识的分格符
String[] date_arr = strDate.split("\\.|\\/|\\-");
try {
if (date_arr.length == 3) {
if (date_arr[2].length() != 4) {
String nowYear = getNowDateTime("yyyy");
date_arr[2] = nowYear.substring(0, 2) + date_arr[2];
}
strReturn = DateUtil.getDateTime("yyyy-MM-dd",
convertStringToDate(combineStringArray(date_arr, "/")));
}
} catch (Exception e) {
return strReturn;
}
return strReturn;
}
/**
* 将字符串数组使用指定的分隔符合并成一个字符串。
* @param array 字符串数组
* @param delim 分隔符,为null的时候使用""作为分隔符(即没有分隔符)
* @return 合并后的字符串
* @since 0.4
*/
public static final String combineStringArray(String[] array, String delim) {
int length = array.length - 1;
if (delim == null) {
delim = "";
}
StringBuffer result = new StringBuffer(length * 8);
for (int i = 0; i < length; i++) {
result.append(array[i]);
result.append(delim);
}
result.append(array[length]);
return result.toString();
}
public static final int getWeekNum(String strWeek) {
int returnValue = 0;
if (strWeek.equals("Mon")) {
returnValue = 1;
} else if (strWeek.equals("Tue")) {
returnValue = 2;
} else if (strWeek.equals("Wed")) {
returnValue = 3;
} else if (strWeek.equals("Thu")) {
returnValue = 4;
} else if (strWeek.equals("Fri")) {
returnValue = 5;
} else if (strWeek.equals("Sat")) {
returnValue = 6;
} else if (strWeek.equals("Sun")) {
returnValue = 0;
} else if (strWeek == null) {
returnValue = 0;
}
return returnValue;
}
/**
* 获取日期字符串中的中文时间表示字符串
* @param strDate
* @return
*/
public static final String getSabreTime(String strDate) {
String strReturn = "";
try {
Date d = DateUtil.str2Date("yyyy-MM-dd HH:mm:ss", CTool.replace(
strDate, "T", " "));
strReturn = DateUtil.date2Str("hh:mm aaa", d);
} catch (Exception e) {
return strReturn;
}
return strReturn;
}
/**
* 获取日期字符串中的中文日期表示字符串
* @param strDate
* @return
*/
public static final String getSabreDate(String strDate) {
String strReturn = "";
try {
String p = null;
if (strDate.length() > 10)
p = "yyyy-MM-dd HH:mm:ss";
else
p = "yyyy-MM-dd";
Date d = DateUtil.str2Date(p, CTool.replace(strDate, "T", " "));
strReturn = DateUtil.date2Str("EEE d-MMM", d);
} catch (Exception e) {
return strReturn;
}
return strReturn;
}
/**
* 获取日期字符串的中文日期时间表示
* @param strDate
* @return
*/
public static final String getSabreDateTime(String strDate) {
String strReturn = "";
try {
String p = null;
if (strDate.length() > 10)
p = "yyyy-MM-dd HH:mm:ss";
else
p = "yyyy-MM-dd";
Date d = DateUtil.str2Date(p, CTool.replace(strDate, "T", " "));
strReturn = DateUtil.date2Str("EEE d-MMM hh:mm aaa", d);
} catch (Exception e) {
return strReturn;
}
return strReturn;
}
/**
*得到格式化后的指定日期
*@param strScheme 格式模式字符串
*@param date 指定的日期值
*@return 格式化后的指定日期,如果有异常产生,返回空串""
*@see java.util.SimpleDateFormat
*/
public static final String getDateTime(Date date, String strScheme) {
String strReturn = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(strScheme);
strReturn = sdf.format(date);
} catch (Exception e) {
strReturn = "";
}
return strReturn;
}
/**
* 获取日期
* @param timeType 时间类型,譬如:Calendar.DAY_OF_YEAR
* @param timenum 时间数字,譬如:-1 昨天,0 今天,1 明天
* @return 日期
*/
public static final Date getDateFromNow(int timeType, int timenum){
Calendar cld = Calendar.getInstance();
cld.set(timeType, cld.get(timeType) + timenum);
return cld.getTime();
}
/**
* 获取日期
* @param timeType 时间类型,譬如:Calendar.DAY_OF_YEAR
* @param timenum 时间数字,譬如:-1 昨天,0 今天,1 明天
* @param format_string 时间格式,譬如:"yyyy-MM-dd HH:mm:ss"
* @return 字符串
*/
public static final String getDateFromNow(int timeType, int timenum, String format_string){
if ((format_string == null)||(format_string.equals("")))
format_string = "yyyy-MM-dd HH:mm:ss";
Calendar cld = Calendar.getInstance();
Date date = null;
DateFormat df = new SimpleDateFormat(format_string);
cld.set(timeType, cld.get(timeType) + timenum);
date = cld.getTime();
return df.format(date);
}
/**
* 获取当前日期的字符串
* @param format_string 时间格式,譬如:"yyyy-MM-dd HH:mm:ss"
* @return 字符串
*/
public static final String getDateNow(String format_string){
if ((format_string == null)||(format_string.equals("")))
format_string = "yyyy-MM-dd HH:mm:ss";
Calendar cld = Calendar.getInstance();
DateFormat df = new SimpleDateFormat(format_string);
return df.format(cld.getTime());
}
}