Hunter0x7c7
2023-08-11 65f55c8ffad5c9e63554bde7082ab2ea2e3ad379
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
package com.github.hunter0x7c7.sync.utils;
 
import org.apache.http.annotation.Immutable;
import org.apache.http.util.Args;
 
import java.lang.ref.SoftReference;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Immutable
public final class DateUtils {
    public static final String PATTERN_Def = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
    public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";
    public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
    private static final String[] DEFAULT_PATTERNS = new String[]{"EEE, dd MMM yyyy HH:mm:ss zzz", "EEE, dd-MMM-yy HH:mm:ss zzz", "EEE MMM d HH:mm:ss yyyy"};
    private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
    public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
 
    public static Date parseDate(String dateValue) {
        return parseDate(dateValue, (String[]) null, (Date) null);
    }
 
    public static Date parseDate(String dateValue, String[] dateFormats) {
        return parseDate(dateValue, dateFormats, (Date) null);
    }
 
    public static Date parseDate(String dateValue, String[] dateFormats, Date startDate) {
        Args.notNull(dateValue, "Date value");
        String[] localDateFormats = dateFormats != null ? dateFormats : DEFAULT_PATTERNS;
        Date localStartDate = startDate != null ? startDate : DEFAULT_TWO_DIGIT_YEAR_START;
        String v = dateValue;
        if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
            v = dateValue.substring(1, dateValue.length() - 1);
        }
 
        String[] arr$ = localDateFormats;
        int len$ = localDateFormats.length;
 
        for (int i$ = 0; i$ < len$; ++i$) {
            String dateFormat = arr$[i$];
            SimpleDateFormat dateParser = DateUtils.DateFormatHolder.formatFor(dateFormat);
            dateParser.set2DigitYearStart(localStartDate);
            ParsePosition pos = new ParsePosition(0);
            Date result = dateParser.parse(v, pos);
            if (pos.getIndex() != 0) {
                return result;
            }
        }
 
        return null;
    }
 
    public static String formatDate(Date date) {
        return formatDate(date, PATTERN_Def);
    }
 
    public static String formatDate(Date date, String pattern) {
        Args.notNull(date, "Date");
        Args.notNull(pattern, "Pattern");
        SimpleDateFormat formatter = DateUtils.DateFormatHolder.formatFor(pattern);
        return formatter.format(date);
    }
 
    public static void clearThreadLocal() {
        DateUtils.DateFormatHolder.clearThreadLocal();
    }
 
    private DateUtils() {
    }
 
    static {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(GMT);
        calendar.set(2000, 0, 1, 0, 0, 0);
        calendar.set(14, 0);
        DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime();
    }
 
    static final class DateFormatHolder {
        private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>> THREADLOCAL_FORMATS = new ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>() {
            protected SoftReference<Map<String, SimpleDateFormat>> initialValue() {
                return new SoftReference(new HashMap());
            }
        };
 
        DateFormatHolder() {
        }
 
        public static SimpleDateFormat formatFor(String pattern) {
            SoftReference<Map<String, SimpleDateFormat>> ref = (SoftReference) THREADLOCAL_FORMATS.get();
            Map<String, SimpleDateFormat> formats = (Map) ref.get();
            if (formats == null) {
                formats = new HashMap();
                THREADLOCAL_FORMATS.set(new SoftReference(formats));
            }
 
            SimpleDateFormat format = (SimpleDateFormat) ((Map) formats).get(pattern);
            if (format == null) {
                format = new SimpleDateFormat(pattern, Locale.getDefault());
                format.setTimeZone(TimeZone.getDefault());
                ((Map) formats).put(pattern, format);
            }
 
            return format;
        }
 
        public static void clearThreadLocal() {
            THREADLOCAL_FORMATS.remove();
        }
    }
}