replace deprecated ISO8601Utils with StdDateFormat (#17052)

This commit is contained in:
Marc Weiß
2024-01-11 11:24:52 +01:00
committed by GitHub
parent be19c35c45
commit 76560e34c9
29 changed files with 320 additions and 137 deletions

View File

@@ -13,11 +13,11 @@
package org.openapitools.client;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonElement;
@@ -31,14 +31,16 @@ import java.io.StringReader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;
/*
* A JSON utility class
@@ -55,6 +57,11 @@ public class JSON {
private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
@SuppressWarnings("unchecked")
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
@@ -334,7 +341,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
return new java.sql.Date(sdf.parse(date).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
@@ -344,7 +351,7 @@ public class JSON {
/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, ISO8601Utils will be used.
* If the dateFormat is null, DateTimeFormatter will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
@@ -369,7 +376,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISO8601Utils.format(date, true);
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
}
out.value(value);
}
@@ -388,7 +395,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
return sdf.parse(date);
} catch (ParseException e) {
throw new JsonParseException(e);
}