@@ -24,10 +24,17 @@ public final class TimeUtils {
2424 private static final ThreadLocal <SimpleDateFormat > SDF_THREAD_LOCAL = new ThreadLocal <>();
2525
2626 private static SimpleDateFormat getDefaultFormat () {
27+ return getDateFormat ("yyyy-MM-dd HH:mm:ss" );
28+ }
29+
30+ @ NonNull
31+ private static SimpleDateFormat getDateFormat (String pattern ) {
2732 SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL .get ();
2833 if (simpleDateFormat == null ) {
29- simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ());
34+ simpleDateFormat = new SimpleDateFormat (pattern , Locale .getDefault ());
3035 SDF_THREAD_LOCAL .set (simpleDateFormat );
36+ } else {
37+ simpleDateFormat .applyPattern (pattern );
3138 }
3239 return simpleDateFormat ;
3340 }
@@ -47,6 +54,17 @@ public static String millis2String(final long millis) {
4754 return millis2String (millis , getDefaultFormat ());
4855 }
4956
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+
5068 /**
5169 * Milliseconds to the formatted time string.
5270 *
@@ -69,6 +87,18 @@ public static long string2Millis(final String time) {
6987 return string2Millis (time , getDefaultFormat ());
7088 }
7189
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+
72102 /**
73103 * Formatted time string to the milliseconds.
74104 *
@@ -96,6 +126,18 @@ public static Date string2Date(final String time) {
96126 return string2Date (time , getDefaultFormat ());
97127 }
98128
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+
99141 /**
100142 * Formatted time string to the date.
101143 *
@@ -123,6 +165,17 @@ public static String date2String(final Date date) {
123165 return date2String (date , getDefaultFormat ());
124166 }
125167
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+
126179 /**
127180 * Date to the formatted time string.
128181 *
@@ -1338,8 +1391,8 @@ public static String getChineseZodiac(final int year) {
13381391 return CHINESE_ZODIAC [year % 12 ];
13391392 }
13401393
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 = {
13431396 "水瓶座" , "双鱼座" , "白羊座" , "金牛座" , "双子座" , "巨蟹座" ,
13441397 "狮子座" , "处女座" , "天秤座" , "天蝎座" , "射手座" , "魔羯座"
13451398 };
@@ -1431,4 +1484,5 @@ private static String millis2FitTimeSpan(long millis, int precision) {
14311484 }
14321485 return sb .toString ();
14331486 }
1487+
14341488}
0 commit comments