Class UtcDateTypeAdapter

java.lang.Object
com.google.gson.TypeAdapter<Date>
com.google.gson.typeadapters.UtcDateTypeAdapter

public final class UtcDateTypeAdapter extends TypeAdapter<Date>
  • Field Details

  • Constructor Details

    • UtcDateTypeAdapter

      public UtcDateTypeAdapter()
  • Method Details

    • write

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

      public Date read(JsonReader in) throws 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<Date>
      Returns:
      the converted Java object. May be null.
      Throws:
      IOException
    • format

      private static String format(Date date, boolean millis, 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(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 Date parse(String date, ParsePosition pos) throws 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:
      ParseException - if the date is not in the appropriate format
    • checkOffset

      private static boolean checkOffset(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(String value, int beginIndex, int endIndex) throws 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:
      NumberFormatException - if the value is not a number