2
2
3
3
package { {invokerPackage} };
4
4
5
+ import com.fasterxml.jackson.databind.util.StdDateFormat;
5
6
import com.google.gson.Gson;
6
7
import com.google.gson.GsonBuilder;
7
8
import com.google.gson.JsonParseException;
8
9
import com.google.gson.TypeAdapter;
9
- import com.google.gson.internal.bind.util.ISO8601Utils;
10
10
import com.google.gson.stream.JsonReader;
11
11
import com.google.gson.stream.JsonWriter;
12
12
import com.google.gson.JsonElement;
@@ -27,14 +27,16 @@ import java.io.StringReader;
27
27
import java.lang.reflect.Type;
28
28
import java.text.DateFormat;
29
29
import java.text.ParseException;
30
- import java.text.ParsePosition;
31
30
import java.time.LocalDate;
32
31
import java.time.OffsetDateTime;
32
+ import java.time.ZoneId;
33
+ import java.time.ZoneOffset;
33
34
import java.time.format.DateTimeFormatter;
34
35
import java.util.Date;
35
36
import java.util.Locale;
36
37
import java.util.Map;
37
38
import java.util.HashMap;
39
+ import java.util.TimeZone;
38
40
39
41
/*
40
42
* A JSON utility class
@@ -57,6 +59,11 @@ public class JSON {
57
59
{ {/jsr310} }
58
60
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
59
61
62
+ private static final StdDateFormat sdf = new StdDateFormat()
63
+ .withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
64
+ .withColonInTimeZone(true);
65
+ private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
66
+
60
67
@SuppressWarnings("unchecked")
61
68
public static GsonBuilder createGson() {
62
69
GsonFireBuilder fireBuilder = new GsonFireBuilder()
@@ -459,7 +466,7 @@ public class JSON {
459
466
if (dateFormat != null) {
460
467
return new java.sql.Date(dateFormat.parse(date).getTime());
461
468
}
462
- return new java.sql.Date(ISO8601Utils .parse(date, new ParsePosition(0) ).getTime());
469
+ return new java.sql.Date(sdf .parse(date).getTime());
463
470
} catch (ParseException e) {
464
471
throw new JsonParseException(e);
465
472
}
@@ -469,7 +476,7 @@ public class JSON {
469
476
470
477
/**
471
478
* Gson TypeAdapter for java.util.Date type
472
- * If the dateFormat is null, ISO8601Utils will be used.
479
+ * If the dateFormat is null, DateTimeFormatter will be used.
473
480
*/
474
481
public static class DateTypeAdapter extends TypeAdapter<Date > {
475
482
@@ -494,7 +501,7 @@ public class JSON {
494
501
if (dateFormat != null) {
495
502
value = dateFormat.format(date);
496
503
} else {
497
- value = ISO8601Utils. format(date, true );
504
+ value = date.toInstant().atOffset(ZoneOffset.UTC). format(dtf );
498
505
}
499
506
out.value(value);
500
507
}
@@ -513,7 +520,7 @@ public class JSON {
513
520
if (dateFormat != null) {
514
521
return dateFormat.parse(date);
515
522
}
516
- return ISO8601Utils .parse(date, new ParsePosition(0) );
523
+ return sdf .parse(date);
517
524
} catch (ParseException e) {
518
525
throw new JsonParseException(e);
519
526
}
0 commit comments