3131import java .text .ParseException ;
3232import java .text .ParsePosition ;
3333import java .time .LocalDate ;
34+ import java .time .LocalDateTime ;
3435import java .time .OffsetDateTime ;
3536import java .time .format .DateTimeFormatter ;
37+ import java .time .format .DateTimeParseException ;
3638import java .util .Date ;
3739import java .util .Map ;
3840import okio .ByteString ;
@@ -51,6 +53,8 @@ public class JSON {
5153 private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter =
5254 new OffsetDateTimeTypeAdapter ();
5355 private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter ();
56+ private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter =
57+ new LocalDateTimeTypeAdapter ();
5458 private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter ();
5559
5660 @ SuppressWarnings ("unchecked" )
@@ -94,6 +98,7 @@ private static Class getClassByDiscriminator(
9498 gsonBuilder .registerTypeAdapter (java .sql .Date .class , sqlDateTypeAdapter );
9599 gsonBuilder .registerTypeAdapter (OffsetDateTime .class , offsetDateTimeTypeAdapter );
96100 gsonBuilder .registerTypeAdapter (LocalDate .class , localDateTypeAdapter );
101+ gsonBuilder .registerTypeAdapter (LocalDateTime .class , localDateTimeTypeAdapter );
97102 gsonBuilder .registerTypeAdapter (byte [].class , byteArrayAdapter );
98103 gsonBuilder .registerTypeAdapterFactory (
99104 new cloud .stackit .sdk .automation .v1api .model .AutomationScheduleTrigger
@@ -401,6 +406,53 @@ public LocalDate read(JsonReader in) throws IOException {
401406 }
402407 }
403408
409+ /** Gson TypeAdapter for JSR310 LocalDateTime type */
410+ public static class LocalDateTimeTypeAdapter extends TypeAdapter <LocalDateTime > {
411+
412+ private DateTimeFormatter formatter ;
413+
414+ public LocalDateTimeTypeAdapter () {
415+ this (DateTimeFormatter .ISO_LOCAL_DATE_TIME );
416+ }
417+
418+ public LocalDateTimeTypeAdapter (DateTimeFormatter formatter ) {
419+ this .formatter = formatter ;
420+ }
421+
422+ public void setFormat (DateTimeFormatter dateFormat ) {
423+ this .formatter = dateFormat ;
424+ }
425+
426+ @ Override
427+ public void write (JsonWriter out , LocalDateTime date ) throws IOException {
428+ if (date == null ) {
429+ out .nullValue ();
430+ } else {
431+ out .value (formatter .format (date ));
432+ }
433+ }
434+
435+ @ Override
436+ public LocalDateTime read (JsonReader in ) throws IOException {
437+ switch (in .peek ()) {
438+ case NULL :
439+ in .nextNull ();
440+ return null ;
441+ default :
442+ String date = in .nextString ();
443+ try {
444+ return LocalDateTime .parse (date , formatter );
445+ } catch (DateTimeParseException e ) {
446+ if (date .length () > 10 && date .charAt (10 ) == ' ' ) {
447+ date = date .substring (0 , 10 ) + 'T' + date .substring (11 );
448+ return LocalDateTime .parse (date , formatter );
449+ }
450+ throw e ;
451+ }
452+ }
453+ }
454+ }
455+
404456 public static void setOffsetDateTimeFormat (DateTimeFormatter dateFormat ) {
405457 offsetDateTimeTypeAdapter .setFormat (dateFormat );
406458 }
@@ -409,6 +461,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) {
409461 localDateTypeAdapter .setFormat (dateFormat );
410462 }
411463
464+ public static void setLocalDateTimeFormat (DateTimeFormatter dateFormat ) {
465+ localDateTimeTypeAdapter .setFormat (dateFormat );
466+ }
467+
412468 /**
413469 * Gson TypeAdapter for java.sql.Date type If the dateFormat is null, a simple "yyyy-MM-dd"
414470 * format will be used (more efficient than SimpleDateFormat).
0 commit comments