Class DefaultDateTypeAdapter<T extends java.util.Date>


  • public final class DefaultDateTypeAdapter<T extends java.util.Date>
    extends TypeAdapter<T>
    This type adapter supports subclasses of date by defining a DefaultDateTypeAdapter.DateType and then using its createAdapterFactory methods.

    Important: Instances of this class (or rather the SimpleDateFormat they use) capture the current default Locale and TimeZone when they are created. Therefore avoid storing factories obtained from DefaultDateTypeAdapter.DateType in static fields, since they only create a single adapter instance and its behavior would then depend on when Gson classes are loaded first, and which default Locale and TimeZone was used at that point.

    • Field Detail

      • DEFAULT_STYLE_FACTORY

        public static final TypeAdapterFactory DEFAULT_STYLE_FACTORY
        Factory for Date adapters which use DateFormat.DEFAULT as style.
      • dateFormats

        private final java.util.List<java.text.DateFormat> dateFormats
        List of 1 or more different date formats used for de-serialization attempts. The first of them is used for serialization as well.
    • Method Detail

      • write

        public void write​(JsonWriter out,
                          java.util.Date value)
                   throws java.io.IOException
        Description copied from class: TypeAdapter
        Writes one JSON value (an array, object, string, number, boolean or null) for value.
        Specified by:
        write in class TypeAdapter<T extends java.util.Date>
        value - the Java object to write. May be null.
        Throws:
        java.io.IOException
      • read

        public T read​(JsonReader in)
               throws java.io.IOException
        Description copied from class: TypeAdapter
        Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object. Returns the converted object.
        Specified by:
        read in class TypeAdapter<T extends java.util.Date>
        Returns:
        the converted Java object. May be null.
        Throws:
        java.io.IOException
      • deserializeToDate

        private java.util.Date deserializeToDate​(JsonReader in)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object