- java.lang.Object
-
- com.google.gson.internal.bind.util.ISO8601Utils
-
public class ISO8601Utils extends java.lang.Object
Utilities methods for manipulating dates in iso8601 format. This is much much faster and GC friendly than using SimpleDateFormat so highly suitable if you (un)serialize lots of date objects. Supported parse format: [yyyy-MM-dd|yyyyMMdd][T(hh:mm[:ss[.sss]]|hhmm[ss[.sss]])]?[Z|[+-]hh[:]mm]]- See Also:
- this specification
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.TimeZone
TIMEZONE_UTC
The UTC timezone, prefetched to avoid more lookups.private static java.lang.String
UTC_ID
ID to represent the 'UTC' string, default timezone since Jackson 2.7
-
Constructor Summary
Constructors Constructor Description ISO8601Utils()
-
Method Summary
All Methods Static 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.static java.lang.String
format(java.util.Date date)
Format a date into 'yyyy-MM-ddThh:mm:ssZ' (default timezone, no milliseconds precision)static java.lang.String
format(java.util.Date date, boolean millis)
Format a date into 'yyyy-MM-ddThh:mm:ss[.sss]Z' (GMT timezone)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 int
indexOfNonDigit(java.lang.String string, int offset)
Returns the index of the first character in the string that is not a digit, starting at offset.private static void
padInt(java.lang.StringBuilder buffer, int value, int length)
Zero pad a number to a specified lengthstatic 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
-
-
-
Field Detail
-
UTC_ID
private static final java.lang.String UTC_ID
ID to represent the 'UTC' string, default timezone since Jackson 2.7- Since:
- 2.7
- See Also:
- Constant Field Values
-
TIMEZONE_UTC
private static final java.util.TimeZone TIMEZONE_UTC
The UTC timezone, prefetched to avoid more lookups.- Since:
- 2.7
-
-
Method Detail
-
format
public static java.lang.String format(java.util.Date date)
Format a date into 'yyyy-MM-ddThh:mm:ssZ' (default timezone, no milliseconds precision)- Parameters:
date
- the date to format- Returns:
- the date formatted as 'yyyy-MM-ddThh:mm:ssZ'
-
format
public static java.lang.String format(java.util.Date date, boolean millis)
Format a date into 'yyyy-MM-ddThh:mm:ss[.sss]Z' (GMT timezone)- Parameters:
date
- the date to formatmillis
- true to include millis precision otherwise false- Returns:
- the date formatted as 'yyyy-MM-ddThh:mm:ss[.sss]Z'
-
format
public 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 (UTC will produce 'Z')- Returns:
- the date formatted as yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
-
parse
public 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
-
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
-
indexOfNonDigit
private static int indexOfNonDigit(java.lang.String string, int offset)
Returns the index of the first character in the string that is not a digit, starting at offset.
-
-