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.StringGMT_IDprivate static java.util.TimeZoneUTC_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 booleancheckOffset(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.Stringformat(java.util.Date date, boolean millis, java.util.TimeZone tz)Format date into yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]private static voidpadInt(java.lang.StringBuilder buffer, int value, int length)Zero pad a number to a specified lengthprivate static java.util.Dateparse(java.lang.String date, java.text.ParsePosition pos)Parse a date from ISO-8601 formatted string.private static intparseInt(java.lang.String value, int beginIndex, int endIndex)Parse an integer located between 2 given offsets in a stringjava.util.Dateread(JsonReader in)Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object.voidwrite(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 static 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:TypeAdapterWrites one JSON value (an array, object, string, number, boolean or null) forvalue.- Specified by:
writein 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:TypeAdapterReads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object. Returns the converted object.- Specified by:
readin 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.ParseExceptionParse 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.NumberFormatExceptionParse 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
-
-