whyclxw
7 天以前 669906b87674d4b60a6fd5ea3a938c636952dbc2
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
package com.whyc.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class ThreadLocalUtil {
 
    public static String TIME_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String TIME_YYYY_MM_DD = "yyyy-MM-dd";
    public static String TIME_YYYY_MM = "yyyy-MM";
    public static String TIME_YYYY_MM_DD_HH_MM_SS_UNION = "yyyyMMddHHmmss";
    public static String TIME_YYYY_MM_DD_HH_MM_SS_UNION2 = "yyyy-MM-dd_HH_mm_ss";
 
    public static ThreadLocal<SimpleDateFormat> sdf = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    public static ThreadLocal<SimpleDateFormat> sdfwithOutday = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy_MM"));
    public static ThreadLocal<SimpleDateFormat> sdfwithday = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
    public static ThreadLocal<SimpleDateFormat> sdfwithtime = ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss"));
    public static ThreadLocal<SimpleDateFormat> sdfwithTABLE=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM"));
    public static ThreadLocal<SimpleDateFormat> sdfwithtime_yyyyMMdd_HH_mm_ss=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss"));
    public static ThreadLocal<SimpleDateFormat> YYYY_MM_DD_HH_MM_SS_UNION=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));
 
    /*
    * flag:1(sdf),2(sdfwithOutday),3(sdfwithday),4(sdfwithtime)
    * */
    public static Date parse(String timeStr,int flag) {
        Date date=null;
        try {
            switch (flag){
                case 1:date=sdf.get().parse(timeStr);break;
                case 2:date=sdfwithOutday.get().parse(timeStr);break;
                case 3:date=sdfwithday.get().parse(timeStr);break;
                case 4:date=sdfwithtime.get().parse(timeStr);break;
                default:date=sdf.get().parse(timeStr);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static String format(Date date,int flag) {
        String timeStr="";
        switch (flag){
            case 1:timeStr=sdf.get().format(date);break;
            case 2:timeStr=sdfwithOutday.get().format(date);break;
            case 3:timeStr=sdfwithday.get().format(date);break;
            case 4:timeStr=sdfwithtime.get().format(date);break;
            case 5:timeStr=sdfwithTABLE.get().format(date);break;
            case 6:timeStr=sdfwithtime_yyyyMMdd_HH_mm_ss.get().format(date);break;
            case 7:timeStr=YYYY_MM_DD_HH_MM_SS_UNION.get().format(date);break;
            default:timeStr=sdf.get().format(date);
        }
        return timeStr;
    }
 
    /**
     *
     * @param timeFormat
     * @param date
     * @return
     */
    public static String format(String timeFormat,Date date) {
        ThreadLocal<SimpleDateFormat> formatThreadLocal = ThreadLocal.withInitial(() -> new SimpleDateFormat(timeFormat));
        SimpleDateFormat format = formatThreadLocal.get();
        return format.format(date);
    }
}