Package com.google.gson.typeadapters
Class UtcDateTypeAdapter
- java.lang.Object
-
- com.google.gson.TypeAdapter<java.util.Date>
-
- com.google.gson.typeadapters.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
-
Constructor Summary
Constructors Constructor Description UtcDateTypeAdapter()
-
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 lengthprivate 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 stringjava.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) forvalue
.-
Methods inherited from class com.google.gson.TypeAdapter
fromJson, fromJson, fromJsonTree, nullSafe, toJson, toJson, toJsonTree
-
-
-
-
Field Detail
-
UTC_TIME_ZONE
private final java.util.TimeZone UTC_TIME_ZONE
-
GMT_ID
private static final java.lang.String GMT_ID
- See Also:
- Constant Field Values
-
-
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) forvalue
.- Specified by:
write
in classTypeAdapter<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 classTypeAdapter<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 formatmillis
- true to include millis precision otherwise falsetz
- 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 paddingvalue
- 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 offsetoffset
- the offset to look for the expected characterexpected
- 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 parsebeginIndex
- the start index for the integer in the stringendIndex
- the end index for the integer in the string- Returns:
- the int
- Throws:
java.lang.NumberFormatException
- if the value is not a number
-
-