@@ -24,10 +24,17 @@ public final class TimeUtils {
24
24
private static final ThreadLocal <SimpleDateFormat > SDF_THREAD_LOCAL = new ThreadLocal <>();
25
25
26
26
private static SimpleDateFormat getDefaultFormat () {
27
+ return getDateFormat ("yyyy-MM-dd HH:mm:ss" );
28
+ }
29
+
30
+ @ NonNull
31
+ private static SimpleDateFormat getDateFormat (String pattern ) {
27
32
SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL .get ();
28
33
if (simpleDateFormat == null ) {
29
- simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ());
34
+ simpleDateFormat = new SimpleDateFormat (pattern , Locale .getDefault ());
30
35
SDF_THREAD_LOCAL .set (simpleDateFormat );
36
+ } else {
37
+ simpleDateFormat .applyPattern (pattern );
31
38
}
32
39
return simpleDateFormat ;
33
40
}
@@ -47,6 +54,17 @@ public static String millis2String(final long millis) {
47
54
return millis2String (millis , getDefaultFormat ());
48
55
}
49
56
57
+ /**
58
+ * Milliseconds to the formatted time string.
59
+ *
60
+ * @param millis The milliseconds.
61
+ * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
62
+ * @return the formatted time string
63
+ */
64
+ public static String millis2String (long millis , @ NonNull final String pattern ) {
65
+ return millis2String (millis , getDateFormat (pattern ));
66
+ }
67
+
50
68
/**
51
69
* Milliseconds to the formatted time string.
52
70
*
@@ -69,6 +87,18 @@ public static long string2Millis(final String time) {
69
87
return string2Millis (time , getDefaultFormat ());
70
88
}
71
89
90
+ /**
91
+ * Formatted time string to the milliseconds.
92
+ * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
93
+ *
94
+ * @param time The formatted time string.
95
+ * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
96
+ * @return the milliseconds
97
+ */
98
+ public static long string2Millis (final String time , @ NonNull final String pattern ) {
99
+ return string2Millis (time , getDateFormat (pattern ));
100
+ }
101
+
72
102
/**
73
103
* Formatted time string to the milliseconds.
74
104
*
@@ -96,6 +126,18 @@ public static Date string2Date(final String time) {
96
126
return string2Date (time , getDefaultFormat ());
97
127
}
98
128
129
+ /**
130
+ * Formatted time string to the date.
131
+ * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
132
+ *
133
+ * @param time The formatted time string.
134
+ * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
135
+ * @return the date
136
+ */
137
+ public static Date string2Date (final String time , @ NonNull final String pattern ) {
138
+ return string2Date (time , getDateFormat (pattern ));
139
+ }
140
+
99
141
/**
100
142
* Formatted time string to the date.
101
143
*
@@ -123,6 +165,17 @@ public static String date2String(final Date date) {
123
165
return date2String (date , getDefaultFormat ());
124
166
}
125
167
168
+ /**
169
+ * Date to the formatted time string.
170
+ *
171
+ * @param date The date.
172
+ * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
173
+ * @return the formatted time string
174
+ */
175
+ public static String date2String (final Date date , @ NonNull final String pattern ) {
176
+ return getDateFormat (pattern ).format (date );
177
+ }
178
+
126
179
/**
127
180
* Date to the formatted time string.
128
181
*
@@ -1338,8 +1391,8 @@ public static String getChineseZodiac(final int year) {
1338
1391
return CHINESE_ZODIAC [year % 12 ];
1339
1392
}
1340
1393
1341
- private static final int [] ZODIAC_FLAGS = {20 , 19 , 21 , 21 , 21 , 22 , 23 , 23 , 23 , 24 , 23 , 22 };
1342
- private static final String [] ZODIAC = {
1394
+ private static final int [] ZODIAC_FLAGS = {20 , 19 , 21 , 21 , 21 , 22 , 23 , 23 , 23 , 24 , 23 , 22 };
1395
+ private static final String [] ZODIAC = {
1343
1396
"水瓶座" , "双鱼座" , "白羊座" , "金牛座" , "双子座" , "巨蟹座" ,
1344
1397
"狮子座" , "处女座" , "天秤座" , "天蝎座" , "射手座" , "魔羯座"
1345
1398
};
@@ -1431,4 +1484,5 @@ private static String millis2FitTimeSpan(long millis, int precision) {
1431
1484
}
1432
1485
return sb .toString ();
1433
1486
}
1487
+
1434
1488
}
0 commit comments