1313import java .time .OffsetDateTime ;
1414import java .time .ZonedDateTime ;
1515import java .time .format .DateTimeFormatter ;
16+ import java .time .format .DateTimeFormatterBuilder ;
1617import java .time .format .DateTimeParseException ;
1718import java .util .function .Function ;
1819
1920import static graphql .scalars .util .Kit .typeName ;
21+ import static java .time .format .DateTimeFormatter .ISO_LOCAL_DATE ;
22+ import static java .time .temporal .ChronoField .HOUR_OF_DAY ;
23+ import static java .time .temporal .ChronoField .MINUTE_OF_HOUR ;
24+ import static java .time .temporal .ChronoField .NANO_OF_SECOND ;
25+ import static java .time .temporal .ChronoField .SECOND_OF_MINUTE ;
2026
2127/**
2228 * Access this via {@link graphql.scalars.ExtendedScalars#DateTime}
@@ -27,6 +33,7 @@ public final class DateTimeScalar {
2733 public static final GraphQLScalarType INSTANCE ;
2834
2935 private DateTimeScalar () {}
36+ private static final DateTimeFormatter customOutputFormatter = getCustomDateTimeFormatter ();
3037
3138 static {
3239 Coercing <OffsetDateTime , String > coercing = new Coercing <OffsetDateTime , String >() {
@@ -45,7 +52,7 @@ public String serialize(Object input) throws CoercingSerializeException {
4552 );
4653 }
4754 try {
48- return DateTimeFormatter . ISO_OFFSET_DATE_TIME .format (offsetDateTime );
55+ return customOutputFormatter .format (offsetDateTime );
4956 } catch (DateTimeException e ) {
5057 throw new CoercingSerializeException (
5158 "Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e .getMessage () + "'."
@@ -102,4 +109,20 @@ private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeExc
102109 .build ();
103110 }
104111
112+ private static DateTimeFormatter getCustomDateTimeFormatter () {
113+ return new DateTimeFormatterBuilder ()
114+ .parseCaseInsensitive ()
115+ .append (ISO_LOCAL_DATE )
116+ .appendLiteral ('T' )
117+ .appendValue (HOUR_OF_DAY , 2 )
118+ .appendLiteral (':' )
119+ .appendValue (MINUTE_OF_HOUR , 2 )
120+ .appendLiteral (':' )
121+ .appendValue (SECOND_OF_MINUTE , 2 )
122+ .appendFraction (NANO_OF_SECOND , 3 , 3 , true )
123+ .appendOffset ("+HH:MM" , "Z" )
124+ .toFormatter ();
125+ }
126+
127+
105128}
0 commit comments