13
13
import java .time .OffsetDateTime ;
14
14
import java .time .ZonedDateTime ;
15
15
import java .time .format .DateTimeFormatter ;
16
+ import java .time .format .DateTimeFormatterBuilder ;
16
17
import java .time .format .DateTimeParseException ;
17
18
import java .util .function .Function ;
18
19
19
20
import 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 ;
20
26
21
27
/**
22
28
* Access this via {@link graphql.scalars.ExtendedScalars#DateTime}
@@ -27,6 +33,7 @@ public final class DateTimeScalar {
27
33
public static final GraphQLScalarType INSTANCE ;
28
34
29
35
private DateTimeScalar () {}
36
+ private static final DateTimeFormatter customOutputFormatter = getCustomDateTimeFormatter ();
30
37
31
38
static {
32
39
Coercing <OffsetDateTime , String > coercing = new Coercing <OffsetDateTime , String >() {
@@ -45,7 +52,7 @@ public String serialize(Object input) throws CoercingSerializeException {
45
52
);
46
53
}
47
54
try {
48
- return DateTimeFormatter . ISO_OFFSET_DATE_TIME .format (offsetDateTime );
55
+ return customOutputFormatter .format (offsetDateTime );
49
56
} catch (DateTimeException e ) {
50
57
throw new CoercingSerializeException (
51
58
"Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e .getMessage () + "'."
@@ -102,4 +109,20 @@ private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeExc
102
109
.build ();
103
110
}
104
111
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
+
105
128
}
0 commit comments