2323import java .time .OffsetDateTime ;
2424import java .time .ZoneOffset ;
2525import java .time .format .DateTimeFormatter ;
26+ import java .time .format .DateTimeFormatterBuilder ;
2627import java .time .format .DateTimeParseException ;
28+ import java .time .temporal .ChronoField ;
2729import java .util .Arrays ;
2830import java .util .Base64 ;
2931import java .util .HashMap ;
@@ -47,11 +49,15 @@ public class UtilsManager {
4749 private static final int BUFFER_SIZE = 8192 ;
4850
4951 private static final DateTimeFormatter OFFSET_DATE_TIME_FORMAT =
50- DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ssXXX" );
52+ new DateTimeFormatterBuilder ()
53+ .appendPattern ("yyyy-MM-dd'T'HH:mm:ss" )
54+ .optionalStart ()
55+ .appendFraction (ChronoField .NANO_OF_SECOND , 0 , 9 , true )
56+ .optionalEnd ()
57+ .appendOffsetId ()
58+ .toFormatter ();
5159 private static final DateTimeFormatter OFFSET_DATE_TIME_FORMAT_WITH_MILLIS =
5260 DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" );
53- private static final DateTimeFormatter OFFSET_DATE_TIME_FORMAT_WITH_MICROS =
54- DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX" );
5561 private static final DateTimeFormatter OFFSET_DATE_FORMAT =
5662 DateTimeFormatter .ofPattern ("yyyy-MM-dd" );
5763
@@ -381,20 +387,11 @@ public static Map<String, String> sanitizeMap(
381387 }
382388
383389 public static OffsetDateTime dateTimeFromString (String dateString ) {
384- DateTimeFormatter [] formatters = {
385- OFFSET_DATE_TIME_FORMAT ,
386- OFFSET_DATE_TIME_FORMAT_WITH_MILLIS ,
387- OFFSET_DATE_TIME_FORMAT_WITH_MICROS
388- };
389-
390- for (DateTimeFormatter formatter : formatters ) {
391- try {
392- return OffsetDateTime .parse (dateString , formatter );
393- } catch (DateTimeParseException e ) {
394- // Ignore and try the next format
395- }
390+ try {
391+ return OffsetDateTime .parse (dateString , OFFSET_DATE_TIME_FORMAT );
392+ } catch (DateTimeParseException e ) {
393+ return null ;
396394 }
397- return null ;
398395 }
399396
400397 public static String dateTimeToString (OffsetDateTime dateTime ) {
@@ -408,19 +405,7 @@ public static OffsetDateTime dateFromString(String dateString) {
408405 return OffsetDateTime .parse (dateString + "T00:00:00Z" );
409406 }
410407 // Otherwise try to parse as full OffsetDateTime
411- DateTimeFormatter [] formatters = {
412- OFFSET_DATE_TIME_FORMAT ,
413- OFFSET_DATE_TIME_FORMAT_WITH_MILLIS ,
414- OFFSET_DATE_TIME_FORMAT_WITH_MICROS
415- };
416- for (DateTimeFormatter formatter : formatters ) {
417- try {
418- return OffsetDateTime .parse (dateString , formatter );
419- } catch (DateTimeParseException e ) {
420- // Ignore and try the next format
421- }
422- }
423- return null ;
408+ return dateTimeFromString (dateString );
424409 } catch (DateTimeParseException e ) {
425410 return null ;
426411 }
0 commit comments