-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathDateFormatUtil.java
40 lines (33 loc) · 1.07 KB
/
DateFormatUtil.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
package wjw.cron.ex;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 使用ThreadLocal以空间换时间解决SimpleDateFormat线程安全问题。
*
* @author wstone
*/
public abstract class DateFormatUtil {
private static final ThreadLocal<Map<String, DateFormat>> _threadLocal = new ThreadLocal<Map<String, DateFormat>>() {
protected Map<String, DateFormat> initialValue() {
return new HashMap<String, DateFormat>();
}
};
public static DateFormat getDateFormat(String pattern) {
DateFormat dateFormat = _threadLocal.get().get(pattern);
if (dateFormat == null) {
dateFormat = new SimpleDateFormat(pattern);
_threadLocal.get().put(pattern, dateFormat);
}
return dateFormat;
}
public static Date parse(String pattern, String textDate) throws ParseException {
return getDateFormat(pattern).parse(textDate);
}
public static String format(String pattern, Date date) {
return getDateFormat(pattern).format(date);
}
}