@@ -862,6 +862,7 @@ public static DateTime parseUTC(String utcString) {
862
862
863
863
if (StrUtil .contains (utcString , CharUtil .DOT )) {
864
864
// 带毫秒,格式类似:2018-09-13T05:34:31.999+08:00
865
+ utcString = normalizeMillSeconds (utcString , "." , "+" );
865
866
return parse (utcString , DatePattern .UTC_MS_WITH_XXX_OFFSET_FORMAT );
866
867
} else {
867
868
// 格式类似:2018-09-13T05:34:31+08:00
@@ -878,6 +879,7 @@ public static DateTime parseUTC(String utcString) {
878
879
879
880
if (StrUtil .contains (utcString , CharUtil .DOT )) {
880
881
// 带毫秒,格式类似:2018-09-13T05:34:31.999-08:00
882
+ utcString = normalizeMillSeconds (utcString , "." , "-" );
881
883
return new DateTime (utcString , DatePattern .UTC_MS_WITH_XXX_OFFSET_FORMAT );
882
884
} else {
883
885
// 格式类似:2018-09-13T05:34:31-08:00
@@ -892,6 +894,7 @@ public static DateTime parseUTC(String utcString) {
892
894
return parse (utcString + ":00" , DatePattern .UTC_SIMPLE_FORMAT );
893
895
} else if (StrUtil .contains (utcString , CharUtil .DOT )) {
894
896
// 可能为: 2021-03-17T06:31:33.99
897
+ utcString = normalizeMillSeconds (utcString , "." , null );
895
898
return parse (utcString , DatePattern .UTC_SIMPLE_MS_FORMAT );
896
899
}
897
900
}
@@ -2337,4 +2340,23 @@ private static String normalize(CharSequence dateStr) {
2337
2340
return builder .toString ();
2338
2341
}
2339
2342
// ------------------------------------------------------------------------ Private method end
2343
+
2344
+ /**
2345
+ * 如果日期中的毫秒部分超出3位,会导致秒数增加,因此只保留前三位
2346
+ *
2347
+ * @param dateStr 日期字符串
2348
+ * @param before 毫秒部分的前一个字符
2349
+ * @param after 毫秒部分的后一个字符
2350
+ * @return 规范之后的毫秒部分
2351
+ */
2352
+ private static String normalizeMillSeconds (String dateStr , CharSequence before , CharSequence after ) {
2353
+ if (StrUtil .isBlank (after )) {
2354
+ String millOrNaco = StrUtil .subPre (StrUtil .subAfter (dateStr , before , true ), 3 );
2355
+ return StrUtil .subBefore (dateStr , before , true ) + before + millOrNaco ;
2356
+ }
2357
+ String millOrNaco = StrUtil .subPre (StrUtil .subBetween (dateStr , before , after ), 3 );
2358
+ return StrUtil .subBefore (dateStr , before , true )
2359
+ + before
2360
+ + millOrNaco + after + StrUtil .subAfter (dateStr , after , true );
2361
+ }
2340
2362
}
0 commit comments