Class UtcDateTypeAdapter


  • public final class UtcDateTypeAdapter
    extends TypeAdapter<java.util.Date>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String GMT_ID  
      private java.util.TimeZone UTC_TIME_ZONE  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean checkOffset​(java.lang.String value, int offset, char expected)
      Check if the expected character exist at the given offset in the value.
      private static java.lang.String format​(java.util.Date date, boolean millis, java.util.TimeZone tz)
      Format date into yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
      private static void padInt​(java.lang.StringBuilder buffer, int value, int length)
      Zero pad a number to a specified length
      private static java.util.Date parse​(java.lang.String date, java.text.ParsePosition pos)
      Parse a date from ISO-8601 formatted string.
      private static int parseInt​(java.lang.String value, int beginIndex, int endIndex)
      Parse an integer located between 2 given offsets in a string
      java.util.Date read​(JsonReader in)
      Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object.
      void write​(JsonWriter out, java.util.Date date)
      Writes one JSON value (an array, object, string, number, boolean or null) for value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UTC_TIME_ZONE

        private final java.util.TimeZone UTC_TIME_ZONE
    • Constructor Detail

      • UtcDateTypeAdapter

        public UtcDateTypeAdapter()
    • Method Detail

      • write

        public void write​(JsonWriter out,
                          java.util.Date date)
                   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<java.util.Date>
        date - the Java object to write. May be null.
        Throws:
        java.io.IOException
      • read

        public java.util.Date 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<java.util.Date>
        Returns:
        the converted Java object. May be null.
        Throws:
        java.io.IOException
      • format

        private static java.lang.String format​(java.util.Date date,
                                               boolean millis,
                                               java.util.TimeZone tz)
        Format date into yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
        Parameters:
        date - the date to format
        millis - true to include millis precision otherwise false
        tz - timezone to use for the formatting (GMT will produce 'Z')
        Returns:
        the date formatted as yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
      • padInt

        private static void padInt​(java.lang.StringBuilder buffer,
                                   int value,
                                   int length)
        Zero pad a number to a specified length
        Parameters:
        buffer - buffer to use for padding
        value - the integer value to pad if necessary.
        length - the length of the string we should zero pad
      • parse

        private static java.util.Date parse​(java.lang.String date,
                                            java.text.ParsePosition pos)
                                     throws java.text.ParseException
        Parse a date from ISO-8601 formatted string. It expects a format [yyyy-MM-dd|yyyyMMdd][T(hh:mm[:ss[.sss]]|hhmm[ss[.sss]])]?[Z|[+-]hh:mm]]
        Parameters:
        date - ISO string to parse in the appropriate format.
        pos - The position to start parsing from, updated to where parsing stopped.
        Returns:
        the parsed date
        Throws:
        java.text.ParseException - if the date is not in the appropriate format
      • checkOffset

        private static boolean checkOffset​(java.lang.String value,
                                           int offset,
                                           char expected)
        Check if the expected character exist at the given offset in the value.
        Parameters:
        value - the string to check at the specified offset
        offset - the offset to look for the expected character
        expected - the expected character
        Returns:
        true if the expected character exist at the given offset
      • parseInt

        private static int parseInt​(java.lang.String value,
                                    int beginIndex,
                                    int endIndex)
                             throws java.lang.NumberFormatException
        Parse an integer located between 2 given offsets in a string
        Parameters:
        value - the string to parse
        beginIndex - the start index for the integer in the string
        endIndex - the end index for the integer in the string
        Returns:
        the int
        Throws:
        java.lang.NumberFormatException - if the value is not a number