Package edu.uiuc.ncsa.security.core.util
Class Iso8601
- java.lang.Object
-
- edu.uiuc.ncsa.security.core.util.Iso8601
-
public class Iso8601 extends Object
This class provides some utilities for creating and parsing ISO 8601 dates. It dates way back to Java 2 or so and should probably be replaced by java.time classes in introduced in Java 8. It is very widely used in Sec-Lib and other projects.Commentary
This has a few things that need to be mentioned. As per contract
date to string conversions always only return a date in UTC, i.e., ending with a "Z"
string to date allows 0 - 3 digits accuracy and various (incl. fractional) time zones which can be notated as +-xxxx or +-xx:xx.
therefore round-tripping of dates is consistent but not quite straightforward. The actual values of the dates never change but string comparisons might fail. For instance, a date in one timezone will in general not be equal as a formatted string to another in a different timezone, but might be completely identical as date objects.
Important technical note: There is a "feature" in java'sSimpleDateFormat
class. In particular, fractions of seconds are handled in an unintiutive way so that errors were being consistently introduced. The methodfixSeconds
fixes this and there are regression tests to catch this now as well.
-
-
Constructor Summary
Constructors Constructor Description Iso8601()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
date2String(long d)
Convert a date to a string in the formatstatic String
date2String(Calendar c)
Convert a date to a string in the formatstatic String
date2String(Date d)
Convert a date to a string in the formatprotected static String
fixSeconds(String rawDate)
This normalizes the date so that it always has 3 places for the number of milliseconds.static DateFormat
getDateFormat()
Provides the DateFormat used by this utility.static DateFormat
getUTCDateFormat()
Provides the date format for UTC used by this utility.static void
main(String[] args)
demo of this classprotected static Calendar
mintCalendar()
Create a new Calendarstatic String[]
splitDate(String rawDate)
static Calendar
string2Date(String s)
Convert a string in the format
-
-
-
Field Detail
-
zeros
protected static String zeros
-
-
Method Detail
-
date2String
public static String date2String(long d)
Convert a date to a string in the format
where "Z" represents UTC.yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
- Parameters:
d
- the date in milliseconds- Returns:
- the string representation of the date
-
date2String
public static String date2String(Date d)
Convert a date to a string in the format
where "Z" represents UTC.yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
- Parameters:
d
- the date- Returns:
- the string representation of the date
-
date2String
public static String date2String(Calendar c)
Convert a date to a string in the format
where "Z" represents UTC.yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
- Parameters:
c
- the date- Returns:
- the string representation of the date
-
getUTCDateFormat
public static DateFormat getUTCDateFormat()
Provides the date format for UTC used by this utility. Normally it is a good idea to use the built in methods of this class and this is provided for backwards compatibility with earlier releases.- Returns:
-
getDateFormat
public static DateFormat getDateFormat()
Provides the DateFormat used by this utility.- Returns:
-
fixSeconds
protected static String fixSeconds(String rawDate) throws ParseException
This normalizes the date so that it always has 3 places for the number of milliseconds. This is unavoidable since there is a serious feature in java's simple date format that will introduce errors.- Parameters:
rawDate
-- Returns:
- Throws:
ParseException
-
splitDate
public static String[] splitDate(String rawDate) throws ParseException
- Throws:
ParseException
-
string2Date
public static Calendar string2Date(String s) throws ParseException
Convert a string in the format
where tzd is either "Z" for UTC or an offset in the format [+-]hh:mm to a date.yyyy-MM-dd'T'HH:mm:ss.S[tzd]
- Parameters:
s
-- Returns:
- Throws:
ParseException
-
mintCalendar
protected static Calendar mintCalendar()
Create a new Calendar- Returns:
-
main
public static void main(String[] args)
demo of this class- Parameters:
args
-
-
-