Package org.apache.directory.api.util
Class GeneralizedTime
- java.lang.Object
-
- org.apache.directory.api.util.GeneralizedTime
-
- All Implemented Interfaces:
Comparable<GeneralizedTime>
public class GeneralizedTime extends Object implements Comparable<GeneralizedTime>
This class represents the generalized time syntax as defined in RFC 4517 section 3.3.13.
The date, time and time zone information is internally backed by an
CalendarobjectLeap seconds are not supported, as
Calendardoes not support leap seconds.3.3.13. Generalized Time A value of the Generalized Time syntax is a character string representing a date and time. The LDAP-specific encoding of a value of this syntax is a restriction of the format defined in [ISO8601], and is described by the following ABNF: GeneralizedTime = century year month day hour [ minute [ second / leap-second ] ] [ fraction ] g-time-zone century = 2(%x30-39) ; "00" to "99" year = 2(%x30-39) ; "00" to "99" month = ( %x30 %x31-39 ) ; "01" (January) to "09" / ( %x31 %x30-32 ) ; "10" to "12" day = ( %x30 %x31-39 ) ; "01" to "09" / ( %x31-32 %x30-39 ) ; "10" to "29" / ( %x33 %x30-31 ) ; "30" to "31" hour = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23" minute = %x30-35 %x30-39 ; "00" to "59" second = ( %x30-35 %x30-39 ) ; "00" to "59" leap-second = ( %x36 %x30 ) ; "60" fraction = ( DOT / COMMA ) 1*(%x30-39) g-time-zone = %x5A ; "Z" / g-differential g-differential = ( MINUS / PLUS ) hour [ minute ] MINUS = %x2D ; minus sign ("-") The <DOT>, <COMMA>, and <PLUS> rules are defined in [RFC4512]. The above ABNF allows character strings that do not represent valid dates (in the Gregorian calendar) and/or valid times (e.g., February 31, 1994). Such character strings SHOULD be considered invalid for this syntax.
The time value represents coordinated universal time (equivalent to Greenwich Mean Time) if the "Z" form of <g-time-zone> is used; otherwise, the value represents a local time in the time zone indicated by <g-differential>. In the latter case, coordinated universal time can be calculated by subtracting the differential from the local time. The "Z" form of <g-time-zone> SHOULD be used in preference to <g-differential>.
If <minute> is omitted, then <fraction> represents a fraction of an hour; otherwise, if <second> and <leap-second> are omitted, then <fraction> represents a fraction of a minute; otherwise, <fraction> represents a fraction of a second. Examples: 199412161032Z 199412160532-0500 Both example values represent the same coordinated universal time: 10:32 AM, December 16, 1994.
The LDAP definition for the Generalized Time syntax is: ( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' ) This syntax corresponds to the GeneralizedTime ASN.1 type from [ASN.1], with the constraint that local time without a differential SHALL NOT be used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classGeneralizedTime.FormatThe format of the generalized time.static classGeneralizedTime.FractionDelimiterThe fraction delimiter of the generalized time.static classGeneralizedTime.TimeZoneFormatThe time zone format of the generalized time.
-
Field Summary
Fields Modifier and Type Field Description private CalendarcalendarThe calendarprivate static DateINFINITEA Date far in the future, when Micro$oft would have vanished for a long time...private GeneralizedTime.FormatupFormatThe user provided formatprivate GeneralizedTime.FractionDelimiterupFractionDelimiterThe user provided fraction delimiterprivate intupFractionLengththe user provided fraction lengthprivate StringupGeneralizedTimeThe user provided valueprivate GeneralizedTime.TimeZoneFormatupTimeZoneFormatThe user provided time zone format
-
Constructor Summary
Constructors Constructor Description GeneralizedTime(long timeInMillis)Creates a new instance of GeneralizedTime by setting the time to an instance of Calendar.GeneralizedTime(String generalizedTime)Creates a new instance of GeneralizedTime, based on the given generalized time string.GeneralizedTime(Calendar calendar)Creates a new instance of GeneralizedTime, based on the given Calendar object.GeneralizedTime(Date date)Creates a new instance of GeneralizedTime by setting the date to an instance of Calendar.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(GeneralizedTime other)Compares this GeneralizedTime object with the specified GeneralizedTime object.booleanequals(Object obj)private StringgetAllDigits(int startIndex)CalendargetCalendar()Gets the calendar.DategetDate()static DategetDate(String zuluTime)Get a Dat einstance from a given StringintgetDay()private GeneralizedTime.FormatgetFormatWithoutFraction(GeneralizedTime.Format f)Gets the corresponding format with fraction.intgetFraction()private StringgetFraction(int startIndex)intgetHour()intgetMinutes()intgetMonth()intgetSeconds()longgetTime()intgetYear()inthashCode()private voidparseDay()private voidparseFractionDelmiter(int fractionDelimiterPos)private voidparseFractionOfHour()private voidparseFractionOfMinute()private voidparseFractionOfSecond()private voidparseHour()private voidparseMinute()private voidparseMonth()private voidparseSecond()private voidparseTimezone(int pos)private voidparseYear()private voidsetUp(Calendar newCalendar)StringtoGeneralizedTime()Returns the string representation of this generalized time.StringtoGeneralizedTime(GeneralizedTime.Format format, GeneralizedTime.FractionDelimiter fractionDelimiter, int fractionLength, GeneralizedTime.TimeZoneFormat timeZoneFormat)Returns the string representation of this generalized time.StringtoGeneralizedTimeWithoutFraction()Returns the string representation of this generalized time.StringtoString()
-
-
-
Field Detail
-
INFINITE
private static final Date INFINITE
A Date far in the future, when Micro$oft would have vanished for a long time...
-
upGeneralizedTime
private String upGeneralizedTime
The user provided value
-
upFormat
private GeneralizedTime.Format upFormat
The user provided format
-
upTimeZoneFormat
private GeneralizedTime.TimeZoneFormat upTimeZoneFormat
The user provided time zone format
-
upFractionDelimiter
private GeneralizedTime.FractionDelimiter upFractionDelimiter
The user provided fraction delimiter
-
upFractionLength
private int upFractionLength
the user provided fraction length
-
calendar
private Calendar calendar
The calendar
-
-
Constructor Detail
-
GeneralizedTime
public GeneralizedTime(Date date)
Creates a new instance of GeneralizedTime by setting the date to an instance of Calendar.- Parameters:
date- the date- See Also:
GeneralizedTime(Calendar)
-
GeneralizedTime
public GeneralizedTime(Calendar calendar)
Creates a new instance of GeneralizedTime, based on the given Calendar object. UsesFormat.YEAR_MONTH_DAY_HOUR_MIN_SEC
as default format andTimeZoneFormat.Z
as default time zone format.- Parameters:
calendar- the calendar containing the date, time and timezone information
-
GeneralizedTime
public GeneralizedTime(long timeInMillis)
Creates a new instance of GeneralizedTime by setting the time to an instance of Calendar.- Parameters:
timeInMillis- the time in milliseconds- See Also:
GeneralizedTime(Calendar)
-
GeneralizedTime
public GeneralizedTime(String generalizedTime) throws ParseException
Creates a new instance of GeneralizedTime, based on the given generalized time string.- Parameters:
generalizedTime- the generalized time- Throws:
ParseException- if the given generalized time can't be parsed.
-
-
Method Detail
-
setUp
private void setUp(Calendar newCalendar)
-
parseTimezone
private void parseTimezone(int pos) throws ParseException- Throws:
ParseException
-
parseFractionOfSecond
private void parseFractionOfSecond() throws ParseException- Throws:
ParseException
-
parseFractionOfMinute
private void parseFractionOfMinute() throws ParseException- Throws:
ParseException
-
parseFractionOfHour
private void parseFractionOfHour() throws ParseException- Throws:
ParseException
-
parseFractionDelmiter
private void parseFractionDelmiter(int fractionDelimiterPos)
-
getFraction
private String getFraction(int startIndex) throws ParseException
- Throws:
ParseException
-
getAllDigits
private String getAllDigits(int startIndex)
-
parseSecond
private void parseSecond() throws ParseException- Throws:
ParseException
-
parseMinute
private void parseMinute() throws ParseException- Throws:
ParseException
-
parseHour
private void parseHour() throws ParseException- Throws:
ParseException
-
parseDay
private void parseDay() throws ParseException- Throws:
ParseException
-
parseMonth
private void parseMonth() throws ParseException- Throws:
ParseException
-
parseYear
private void parseYear() throws ParseException- Throws:
ParseException
-
toGeneralizedTime
public String toGeneralizedTime()
Returns the string representation of this generalized time. This method uses the same format as the user provided format.- Returns:
- the string representation of this generalized time
-
toGeneralizedTimeWithoutFraction
public String toGeneralizedTimeWithoutFraction()
Returns the string representation of this generalized time. This method uses the same format as the user provided format.- Returns:
- the string representation of this generalized time
-
getFormatWithoutFraction
private GeneralizedTime.Format getFormatWithoutFraction(GeneralizedTime.Format f)
Gets the corresponding format with fraction.- Parameters:
f- the format- Returns:
- the corresponding format without fraction
-
toGeneralizedTime
public String toGeneralizedTime(GeneralizedTime.Format format, GeneralizedTime.FractionDelimiter fractionDelimiter, int fractionLength, GeneralizedTime.TimeZoneFormat timeZoneFormat)
Returns the string representation of this generalized time.- Parameters:
format- the target formatfractionDelimiter- the target fraction delimiter, may be nullfractionLength- the fraction lengthtimeZoneFormat- the target time zone format- Returns:
- the string
-
getCalendar
public Calendar getCalendar()
Gets the calendar. It could be used to manipulate thisGeneralizedTimesettings.- Returns:
- the calendar
-
compareTo
public int compareTo(GeneralizedTime other)
Compares this GeneralizedTime object with the specified GeneralizedTime object.- Specified by:
compareToin interfaceComparable<GeneralizedTime>- Parameters:
other- the other GeneralizedTime object- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
- See Also:
Comparable.compareTo(java.lang.Object)
-
getTime
public long getTime()
- Returns:
- A Date representing the time as milliseconds
-
getDate
public Date getDate()
- Returns:
- A Date representing the time
-
getYear
public int getYear()
- Returns:
- The year part of the date
-
getMonth
public int getMonth()
- Returns:
- The month part of the date
-
getDay
public int getDay()
- Returns:
- The day part of the date
-
getHour
public int getHour()
- Returns:
- The hours part of the date
-
getMinutes
public int getMinutes()
- Returns:
- The minutes part of the date
-
getSeconds
public int getSeconds()
- Returns:
- The seconds part of the date
-
getFraction
public int getFraction()
- Returns:
- The fractional (ie, milliseconds) part of the date
-
getDate
public static Date getDate(String zuluTime) throws ParseException
Get a Dat einstance from a given String- Parameters:
zuluTime- The time as a String- Returns:
- A Date instance
- Throws:
ParseException- If the String is not a valid date
-
-