fangyuan
2022-11-17 059eebfe1c54750e74e290ed7503e2cbf3f2f740
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
package com.ycl.utils.common;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 *  日期线程类
 */
public final class ThreadDateUtil {
 
    /**
     *  日期格式1
     */
    public static final String                          yyyyMMddHHmmsss = "yyyyMMddHHmmsss";
    /**
     *  日期格式2
     */
    public static final String                          yyMMddHHmmsss   = "yyMMddHHmmsss";
 
    /**
     *   默认格式
     */
    private static final String                         DEFAULT_FORMAT  = "yyyy-MM-dd HH:mm:ss";
 
    /**
     *  格式化 线程集
     */
    private static Map<String, ThreadLocal<DateFormat>> threadLocalMap  = new HashMap<>();
 
    /**
     *    获取日期格式化工具对象
     *    线程安全
     * @return
     */
    public static DateFormat getDateFormat() {
        return getDateFormat(DEFAULT_FORMAT);
    }
 
    /**
     *  获取日期格式化工具对象
     *      线程安全
     * @param format    格式      可取些工具类中的常量格式,默认为yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static DateFormat getDateFormat(String format) {
        ThreadLocal<DateFormat> threadLocal = threadLocalMap.get(format);
        DateFormat df = null;
        if (threadLocal == null) {
            threadLocal = new ThreadLocal<DateFormat>();
            df = new SimpleDateFormat(format);
            threadLocal.set(df);
            threadLocalMap.put(format, threadLocal);
        } else {
            df = threadLocal.get();
            if (df == null) {
                df = new SimpleDateFormat(format);
                threadLocal.set(df);
                threadLocalMap.put(format, threadLocal);
            }
        }
        return df;
    }
 
    public static String formatDate(Date date) throws ParseException {
        return getDateFormat().format(date);
    }
 
    public static Date parse(String strDate) throws ParseException {
        return getDateFormat().parse(strDate);
    }
 
 
 
 
}