DateUtils.java
3.57 KB
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
package cn.com.glsx.stdinterface.modules.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 日期处理
*
* @author yll
*/
@Slf4j
public class DateUtils extends com.glsx.plat.common.utils.DateUtils {
public final static String[] pattern = {"yyyy-MM-dd HH:mm:ss"};
public final static long defaultTimeDiff = -1l;
public static String pattern19 = "yyyy-MM-dd HH:mm:ss";
public static String pattern14 = "yyyyMMddHHmmss";
public static String pattern10 = "yyyy-MM-dd";
public static String pattern8 = "yyyyMMdd";
public static String pattern6 = "HHmmss";
private static final Map<String, ThreadLocal<SimpleDateFormat>> pool
= new HashMap<String, ThreadLocal<SimpleDateFormat>>();
private static final Object lock = new Object();
public static SimpleDateFormat getDateFormat(String pattern) {
ThreadLocal<SimpleDateFormat> tl = pool.get(pattern);
if (tl == null) {
synchronized (lock) {
tl = pool.get(pattern);
if (tl == null) {
final String p = pattern;
tl = new ThreadLocal<SimpleDateFormat>() {
protected synchronized SimpleDateFormat initialValue() {
return new SimpleDateFormat(p);
}
};
pool.put(p, tl);
}
}
}
return tl.get();
}
/**
* 时间字符串转日期
* @param dateStr
* @param pattern
* @return
*/
public static Date toDate(String dateStr, String pattern) {
if (StringUtils.isBlank(dateStr)) {
return null;
}
try {
return getDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
log.error("dateUtil parseException", e);
}
return null;
}
/**
* 日期转时间字符串
* @param date
* @param pattern
* @return
*/
public static String toString(Date date, String pattern) {
return getDateFormat(pattern).format(date);
}
public static String nowString(String pattern) {
return getDateFormat(pattern).format(new Date());
}
/**
* 格式化传的时间为 X天X小时X分
*
* @param durationTime 时间戳
* @return 天 小时 分
*/
public static String formatTimeInSec2(long durationTime) {
if (durationTime > 0) {
int s = (int) durationTime;
int day = s / (3600 * 24);
s = s % (3600 * 24);
int hour = s / 3600;
s = s % 3600;
int min = s / 60;
s = s % 60;
int sec = s;
return day + "天" + hour + "小时" + min + "分";
} else {
return "0";
}
}
/**
* 计算当前时间与指定时间的时间差
*
* @param time
* @return
* @author lidh
*/
@SuppressWarnings("null")
public static long getTimeDifference(String time) {
if (StringUtils.isBlank(time)) {
return defaultTimeDiff;
}
try {
Date parseDate = DateUtils.parseDate(time, pattern);
long difference = System.currentTimeMillis() - parseDate.getTime();
return difference;
} catch (ParseException e) {
e.printStackTrace();
}
return defaultTimeDiff;
}
}