Top Description Fields Constructors Methods
com.google.gson.internal.bind.util

public Class ISO8601Utils

extends Object
Class Inheritance
Annotations
@SuppressWarnings:MemberName
Imports
java.text.ParseException, .ParsePosition, java.util.Calendar, .Date, .GregorianCalendar, .Locale, .TimeZone

Utilities methods for manipulating dates in iso8601 format. This is 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

Modifier and TypeField and Description
private static final TimeZone
TIMEZONE_UTC

The UTC timezone, prefetched to avoid more lookups.

private static final String
UTC_ID

ID to represent the 'UTC' string, default timezone since Jackson 2.7

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static boolean

Returns:

true if the expected character exist at the given offset
checkOffset
(String
the string to check at the specified offset
value
,
int
the offset to look for the expected character
offset
,
char
the expected character
expected
)

Check if the expected character exist at the given offset in the value.

public static String

Returns:

the date formatted as 'yyyy-MM-ddThh:mm:ssZ'
format
(Date
the date to format
date
)

Format a date into 'yyyy-MM-ddThh:mm:ssZ' (default timezone, no milliseconds precision)

public static String

Returns:

the date formatted as 'yyyy-MM-ddThh:mm:ss[.sss]Z'
format
(Date
the date to format
date
,
boolean
true to include millis precision otherwise false
millis
)

Format a date into 'yyyy-MM-ddThh:mm:ss[.sss]Z' (GMT timezone)

public static String

Returns:

the date formatted as yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
format
(Date
the date to format
date
,
boolean
true to include millis precision otherwise false
millis
,
TimeZone
timezone to use for the formatting (UTC will produce 'Z')
tz
)

Format date into yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]

private static int
indexOfNonDigit(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(StringBuilder
buffer to use for padding
buffer
,
int
the integer value to pad if necessary.
value
,
int
the length of the string we should zero pad
length
)

Zero pad a number to a specified length

public static Date

Returns:

the parsed date
parse
(String
ISO string to parse in the appropriate format.
date
,
ParsePosition
The position to start parsing from, updated to where parsing stopped.
pos
)

Parse a date from ISO-8601 formatted string.

private static int

Returns:

the int
parseInt
(String
the string to parse
value
,
int
the start index for the integer in the string
beginIndex
,
int
the end index for the integer in the string
endIndex
)

Parse an integer located between 2 given offsets in a string

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

TIMEZONE_UTCback to summary
private static final TimeZone TIMEZONE_UTC

The UTC timezone, prefetched to avoid more lookups.

Since
2.7
UTC_IDback to summary
private static final String UTC_ID

ID to represent the 'UTC' string, default timezone since Jackson 2.7

Since
2.7

Constructor Detail

ISO8601Utilsback to summary
private ISO8601Utils()

Method Detail

checkOffsetback to summary
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:String

the string to check at the specified offset

offset:int

the offset to look for the expected character

expected:char

the expected character

Returns:boolean

true if the expected character exist at the given offset

formatback to summary
public static String format(Date date)

Format a date into 'yyyy-MM-ddThh:mm:ssZ' (default timezone, no milliseconds precision)

Parameters
date:Date

the date to format

Returns:String

the date formatted as 'yyyy-MM-ddThh:mm:ssZ'

formatback to summary
public static String format(Date date, boolean millis)

Format a date into 'yyyy-MM-ddThh:mm:ss[.sss]Z' (GMT timezone)

Parameters
date:Date

the date to format

millis:boolean

true to include millis precision otherwise false

Returns:String

the date formatted as 'yyyy-MM-ddThh:mm:ss[.sss]Z'

formatback to summary
public static String format(Date date, boolean millis, TimeZone tz)

Format date into yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]

Parameters
date:Date

the date to format

millis:boolean

true to include millis precision otherwise false

tz:TimeZone

timezone to use for the formatting (UTC will produce 'Z')

Returns:String

the date formatted as yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]

indexOfNonDigitback to summary
private static int indexOfNonDigit(String string, int offset)

Returns the index of the first character in the string that is not a digit, starting at offset.

padIntback to summary
private static void padInt(StringBuilder buffer, int value, int length)

Zero pad a number to a specified length

Parameters
buffer:StringBuilder

buffer to use for padding

value:int

the integer value to pad if necessary.

length:int

the length of the string we should zero pad

parseback to summary
public 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:String

ISO string to parse in the appropriate format.

pos:ParsePosition

The position to start parsing from, updated to where parsing stopped.

Returns:Date

the parsed date

Exceptions
ParseException:
if the date is not in the appropriate format
parseIntback to summary
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:String

the string to parse

beginIndex:int

the start index for the integer in the string

endIndex:int

the end index for the integer in the string

Returns:int

the int

Exceptions
NumberFormatException:
if the value is not a number