@@ -874,6 +874,7 @@ public static DateTime parseUTC(String utcString) {
874
874
875
875
if (StrUtil .contains (utcString , CharUtil .DOT )) {
876
876
// 带毫秒,格式类似:2018-09-13T05:34:31.999+08:00
877
+ utcString = normalizeMillSeconds (utcString , "." , "+" );
877
878
return parse (utcString , DatePattern .UTC_MS_WITH_XXX_OFFSET_FORMAT );
878
879
} else {
879
880
// 格式类似:2018-09-13T05:34:31+08:00
@@ -890,6 +891,7 @@ public static DateTime parseUTC(String utcString) {
890
891
891
892
if (StrUtil .contains (utcString , CharUtil .DOT )) {
892
893
// 带毫秒,格式类似:2018-09-13T05:34:31.999-08:00
894
+ utcString = normalizeMillSeconds (utcString , "." , "-" );
893
895
return new DateTime (utcString , DatePattern .UTC_MS_WITH_XXX_OFFSET_FORMAT );
894
896
} else {
895
897
// 格式类似:2018-09-13T05:34:31-08:00
@@ -904,6 +906,7 @@ public static DateTime parseUTC(String utcString) {
904
906
return parse (utcString + ":00" , DatePattern .UTC_SIMPLE_FORMAT );
905
907
} else if (StrUtil .contains (utcString , CharUtil .DOT )) {
906
908
// 可能为: 2021-03-17T06:31:33.99
909
+ utcString = normalizeMillSeconds (utcString , "." , null );
907
910
return parse (utcString , DatePattern .UTC_SIMPLE_MS_FORMAT );
908
911
}
909
912
}
@@ -2349,4 +2352,23 @@ private static String normalize(CharSequence dateStr) {
2349
2352
return builder .toString ();
2350
2353
}
2351
2354
// ------------------------------------------------------------------------ Private method end
2355
+
2356
+ /**
2357
+ * 如果日期中的毫秒部分超出3位,会导致秒数增加,因此只保留前三位
2358
+ *
2359
+ * @param dateStr 日期字符串
2360
+ * @param before 毫秒部分的前一个字符
2361
+ * @param after 毫秒部分的后一个字符
2362
+ * @return 规范之后的毫秒部分
2363
+ */
2364
+ private static String normalizeMillSeconds (String dateStr , CharSequence before , CharSequence after ) {
2365
+ if (StrUtil .isBlank (after )) {
2366
+ String millOrNaco = StrUtil .subPre (StrUtil .subAfter (dateStr , before , true ), 3 );
2367
+ return StrUtil .subBefore (dateStr , before , true ) + before + millOrNaco ;
2368
+ }
2369
+ String millOrNaco = StrUtil .subPre (StrUtil .subBetween (dateStr , before , after ), 3 );
2370
+ return StrUtil .subBefore (dateStr , before , true )
2371
+ + before
2372
+ + millOrNaco + after + StrUtil .subAfter (dateStr , after , true );
2373
+ }
2352
2374
}
0 commit comments