Package org.restlet.engine.util
Class InternetDateFormat
- java.lang.Object
-
- java.text.Format
-
- java.text.DateFormat
-
- org.restlet.engine.util.InternetDateFormat
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class InternetDateFormat extends java.text.DateFormatThis class handles Internet date/time strings in accordance with RFC 3339. It provides static methods to convert from various Java constructs (long, Date, and Calendar) to RFC 3339 format strings and to parse these strings back into the same Java constructs.In addition to the static utility methods, this class also wraps a Calendar object allowing this class to be used as a value object in place of a Java construct.
Strings are parsed in accordance with the RFC 3339 format:
YYYY-MM-DD(T|t|\s)hh:mm:ss[.ddd][tzd]
Thetzdrepresents the time zone designator and is either an upper or lower case 'Z' indicating UTC or a signedhh:mmoffset.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.CalendarcalThe Calendar object that allows this class to act as a value holder.private static java.text.DecimalFormatdf2private static java.text.DecimalFormatdf4private static java.util.regex.PatternpatternThe Regex pattern to match.private static longserialVersionUIDstatic java.util.TimeZoneUTCA time zone with zero offset and no DST.-
Fields inherited from class java.text.DateFormat
AM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD
-
-
Constructor Summary
Constructors Constructor Description InternetDateFormat()Creates a new InternetDateFormat object set to the current time using the UTC (Z) time zone.InternetDateFormat(long time)Creates a new InternetDateFormat object initialized from a millisecond time value using the UTC (Z) time zone.InternetDateFormat(long time, java.util.TimeZone zone)Creates a new InternetDateFormat object initialized from a millisecond time value using the specified time zone.InternetDateFormat(java.lang.String s)Creates a new InternetDateFormat object by parsing an RFC 3339 date/time string.InternetDateFormat(java.util.Calendar cal)Creates a new InternetDateFormat object initialized from a Calendar object.InternetDateFormat(java.util.Date date)Creates a new InternetDateFormat object initialized from a Date object using the UTC (Z) time zone.InternetDateFormat(java.util.Date date, java.util.TimeZone zone)Creates a new InternetDateFormat object initialized from a Date object using the specified time zone.InternetDateFormat(java.util.TimeZone zone)Creates a new InternetDateFormat object set to the current time using the specified time zone.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringBufferformat(java.util.Date date, java.lang.StringBuffer toAppendTo, java.text.FieldPosition fieldPosition)java.util.CalendargetCalendar()Gets the Calendar object wrapped by this InternetDateFormat object.java.util.DategetDate()Gets the value of this InternetDateFormat object as a Date object.longgetTime()Gets the value of this InternetDateFormat object as millisecond time value.static java.lang.Stringnow()Returns the current date and time as an RFC 3339 date/time string using the UTC (Z) time zone.static java.lang.Stringnow(java.util.TimeZone zone)Returns the current date and time as an RFC 3339 date/time string using the specified time zone.java.util.Dateparse(java.lang.String source)java.util.Dateparse(java.lang.String source, java.text.ParsePosition pos)private static voidparse(java.lang.String s, java.util.Calendar cal)Our private parse utility that parses the string, clears the calendar, and then sets the fields.static java.util.CalendarparseCalendar(java.lang.String s)Parses an RFC 3339 date/time string to a Calendar object.static java.util.DateparseDate(java.lang.String s)Parses an RFC 3339 date/time string to a Date object.static longparseTime(java.lang.String s)Parses an RFC 3339 date/time string to a millisecond time value.java.lang.StringtoString()Converts this InternetDateFormat object to an RFC 3339 date/time string.static java.lang.StringtoString(long time)Converts the specified millisecond time value to an RFC 3339 date/time string using the UTC (Z) time zone.static java.lang.StringtoString(long time, java.util.TimeZone zone)Converts the specified millisecond time value to an RFC 3339 date/time string using the specified time zone.static java.lang.StringtoString(java.util.Calendar cal)Converts the specified Calendar object to an RFC 3339 date/time string.static java.lang.StringtoString(java.util.Date date)Converts the specified Date object to an RFC 3339 date/time string using the UTC (Z) time zone.static java.lang.StringtoString(java.util.Date date, java.util.TimeZone zone)Converts the specified Date object to an RFC 3339 date/time string using the specified time zone.static InternetDateFormatvalueOf(long time)Creates a new InternetDateFormat object from the specified millisecond time value using the UTC (Z) time zone.static InternetDateFormatvalueOf(long time, java.util.TimeZone zone)Creates a new InternetDateFormat object from the specified millisecond time value using the specified time zone.static InternetDateFormatvalueOf(java.lang.String s)Creates a new InternetDateFormat object by parsing an RFC 3339 date/time string.static InternetDateFormatvalueOf(java.util.Date date)Creates a new InternetDateFormat object from the specified Date object using the UTC (Z) time zone.static InternetDateFormatvalueOf(java.util.Date date, java.util.TimeZone zone)Creates a new InternetDateFormat object from the specified Date object using the specified time zone.-
Methods inherited from class java.text.DateFormat
clone, equals, format, format, getAvailableLocales, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getNumberFormat, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, hashCode, isLenient, parseObject, setCalendar, setLenient, setNumberFormat, setTimeZone
-
-
-
-
Field Detail
-
df2
private static volatile java.text.DecimalFormat df2
-
df4
private static volatile java.text.DecimalFormat df4
-
pattern
private static volatile java.util.regex.Pattern pattern
The Regex pattern to match.
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
UTC
public static final java.util.TimeZone UTC
A time zone with zero offset and no DST.
-
cal
private java.util.Calendar cal
The Calendar object that allows this class to act as a value holder.
-
-
Constructor Detail
-
InternetDateFormat
public InternetDateFormat()
Creates a new InternetDateFormat object set to the current time using the UTC (Z) time zone.
-
InternetDateFormat
public InternetDateFormat(java.util.Calendar cal)
Creates a new InternetDateFormat object initialized from a Calendar object. The specified calendar object is cloned thereby isolating this InternetDateFormat object from any changes made to the specified calendar object after calling this constructor.- Parameters:
cal- the Calendar object
-
InternetDateFormat
public InternetDateFormat(java.util.Date date)
Creates a new InternetDateFormat object initialized from a Date object using the UTC (Z) time zone.- Parameters:
date- the Date object
-
InternetDateFormat
public InternetDateFormat(java.util.Date date, java.util.TimeZone zone)Creates a new InternetDateFormat object initialized from a Date object using the specified time zone.- Parameters:
date- the Date objectzone- the time zone to use
-
InternetDateFormat
public InternetDateFormat(long time)
Creates a new InternetDateFormat object initialized from a millisecond time value using the UTC (Z) time zone.- Parameters:
time- the millisecond time value
-
InternetDateFormat
public InternetDateFormat(long time, java.util.TimeZone zone)Creates a new InternetDateFormat object initialized from a millisecond time value using the specified time zone.- Parameters:
time- the millisecond time valuezone- the time zone to use
-
InternetDateFormat
public InternetDateFormat(java.lang.String s)
Creates a new InternetDateFormat object by parsing an RFC 3339 date/time string.- Parameters:
s- the string to parse- Throws:
java.lang.IllegalArgumentException- if the string is not a valid RFC 3339 date/time string
-
InternetDateFormat
public InternetDateFormat(java.util.TimeZone zone)
Creates a new InternetDateFormat object set to the current time using the specified time zone.- Parameters:
zone- the time zone to use
-
-
Method Detail
-
now
public static java.lang.String now()
Returns the current date and time as an RFC 3339 date/time string using the UTC (Z) time zone.- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
now
public static java.lang.String now(java.util.TimeZone zone)
Returns the current date and time as an RFC 3339 date/time string using the specified time zone.- Parameters:
zone- the time zone to use- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
parse
private static void parse(java.lang.String s, java.util.Calendar cal)Our private parse utility that parses the string, clears the calendar, and then sets the fields.- Parameters:
s- the string to parsecal- the calendar object to populate- Throws:
java.lang.IllegalArgumentException- if the string is not a valid RFC 3339 date/time string
-
parseCalendar
public static java.util.Calendar parseCalendar(java.lang.String s)
Parses an RFC 3339 date/time string to a Calendar object.- Parameters:
s- the string to parse- Returns:
- the Calendar object
- Throws:
java.lang.IllegalArgumentException- if the string is not a valid RFC 3339 date/time string
-
parseDate
public static java.util.Date parseDate(java.lang.String s)
Parses an RFC 3339 date/time string to a Date object.- Parameters:
s- the string to parse- Returns:
- the Date object
- Throws:
java.lang.IllegalArgumentException- if the string is not a valid RFC 3339 date/time string
-
parseTime
public static long parseTime(java.lang.String s)
Parses an RFC 3339 date/time string to a millisecond time value.- Parameters:
s- the string to parse- Returns:
- the millisecond time value
- Throws:
java.lang.IllegalArgumentException- if the string is not a valid RFC 3339 date/time string
-
toString
public static java.lang.String toString(java.util.Calendar cal)
Converts the specified Calendar object to an RFC 3339 date/time string. Unlike the toString methods for Date and long, no additional variant of this method taking a time zone is provided since the time zone is built into the Calendar object.- Parameters:
cal- the Calendar object- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
toString
public static java.lang.String toString(java.util.Date date)
Converts the specified Date object to an RFC 3339 date/time string using the UTC (Z) time zone.- Parameters:
date- the Date object- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
toString
public static java.lang.String toString(java.util.Date date, java.util.TimeZone zone)Converts the specified Date object to an RFC 3339 date/time string using the specified time zone.- Parameters:
date- the Date objectzone- the time zone to use- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
toString
public static java.lang.String toString(long time)
Converts the specified millisecond time value to an RFC 3339 date/time string using the UTC (Z) time zone.- Parameters:
time- the millisecond time value- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
toString
public static java.lang.String toString(long time, java.util.TimeZone zone)Converts the specified millisecond time value to an RFC 3339 date/time string using the specified time zone.- Parameters:
time- the millisecond time valuezone- the time zone to use- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
valueOf
public static InternetDateFormat valueOf(java.util.Date date)
Creates a new InternetDateFormat object from the specified Date object using the UTC (Z) time zone.- Parameters:
date- the Date object- Returns:
- the InternetDateFormat object
-
valueOf
public static InternetDateFormat valueOf(java.util.Date date, java.util.TimeZone zone)
Creates a new InternetDateFormat object from the specified Date object using the specified time zone.- Parameters:
date- the Date objectzone- the time zone to use- Returns:
- the InternetDateFormat object
-
valueOf
public static InternetDateFormat valueOf(long time)
Creates a new InternetDateFormat object from the specified millisecond time value using the UTC (Z) time zone.- Parameters:
time- the millisecond time value- Returns:
- the InternetDateFormat object
-
valueOf
public static InternetDateFormat valueOf(long time, java.util.TimeZone zone)
Creates a new InternetDateFormat object from the specified millisecond time value using the specified time zone.- Parameters:
time- the millisecond time valuezone- the time zone to use- Returns:
- the InternetDateFormat object
-
valueOf
public static InternetDateFormat valueOf(java.lang.String s)
Creates a new InternetDateFormat object by parsing an RFC 3339 date/time string.- Parameters:
s- the string to parse- Returns:
- the InternetDateFormat object
- Throws:
java.lang.IllegalArgumentException- if the string is not a valid RFC 3339 date/time string
-
format
public java.lang.StringBuffer format(java.util.Date date, java.lang.StringBuffer toAppendTo, java.text.FieldPosition fieldPosition)- Specified by:
formatin classjava.text.DateFormat
-
getCalendar
public java.util.Calendar getCalendar()
Gets the Calendar object wrapped by this InternetDateFormat object.- Overrides:
getCalendarin classjava.text.DateFormat- Returns:
- the cloned Calendar object
-
getDate
public java.util.Date getDate()
Gets the value of this InternetDateFormat object as a Date object.- Returns:
- the Date object
-
getTime
public long getTime()
Gets the value of this InternetDateFormat object as millisecond time value.- Returns:
- the millisecond time value
-
parse
public java.util.Date parse(java.lang.String source) throws java.text.ParseException- Overrides:
parsein classjava.text.DateFormat- Throws:
java.text.ParseException
-
parse
public java.util.Date parse(java.lang.String source, java.text.ParsePosition pos)- Specified by:
parsein classjava.text.DateFormat
-
toString
public java.lang.String toString()
Converts this InternetDateFormat object to an RFC 3339 date/time string.- Overrides:
toStringin classjava.lang.Object- Returns:
- an RFC 3339 date/time string (does not include milliseconds)
-
-