Package com.oss.asn1

Class GeneralizedTime

All Implemented Interfaces:
Serializable, Cloneable

public class GeneralizedTime extends AbstractTime
The GeneralizedTime class represents the ASN.1 GeneralizedTime type.
Since:
ASN.1/Java 6.0
See Also:
  • Field Summary

    Fields inherited from class com.oss.asn1.AbstractData

    EQUALS, GREATER_THAN, LESS_THAN
  • Constructor Summary

    Constructors
    Constructor
    Description
    The default constructor.
    GeneralizedTime(int[] components, boolean isUTCTime)
    Construct an instance of this class from an int array.
    GeneralizedTime(int year, int month, int day, int hour, int minute, int second, int minDiff, int millisecond, boolean isUTCTime)
    Construct from individual time components.
    Construct an instance of this class from a String containing value notation of the GeneralizedTime.
  • Method Summary

    Modifier and Type
    Method
    Description
    Clone 'this' object.
    final int
    Compare 'this' object to another GeneralizedTime object to determine whether the contents of this object is less than, equal to, or greater than the contents of other object.
    final int
    compareTo(GeneralizedTime that, TimeZone zone, Locale locale)
    Compare 'this' object to another object of the same class to determine whether the contents of this object is less than, equal to, or greater than the contents of other object.
    final boolean
    Compare 'this' object to another GeneralizedTime object to see if their contents are the same.
    final String
    Takes the instance of AbstractTime and converts it to the formatted string, suitable for the encoding.
    final boolean
    Determine whether this GeneralizedTime represents a UTCTime value (with 'Z').
    final int
    Get the millisecond component of 'this' object.
    Returns the name of ASN.1 type this java class represents.
    final int[]
    Get an int array containing the components of 'this' object.
    Takes formatted string that contains the value of GeneralizedTime, splits the string to the components and initializes the instance of the AbstractTime class, passed as a parameter.
    final void
    setIsUTCTime(boolean isUTCTime)
    Specify that this GeneralizedTime should represent UTCTime.
    final void
    setMillisecond(int millisecond)
    Set the millisecond component of 'this' object.
    final void
    setMinuteDifferential(int minDiff)
    Set the minute-differential component of 'this' object if the isUTCTime flag is false.
    final void
    setValue(int[] components, boolean isUTCTime)
    Set the value of a GeneralizedTime from an array of int that contains the year, month, day, hour, minute, second, minute-differential and millisecond.
    void
    Set an instance of this class from a String containing value notation of the GeneralizedTime.
    final Date
    toDate(Calendar calendar)
    Get a Java Date object primed with the data from 'this' AbstractTime object using the calendar "calendar".
    boolean
    Overrides basic version of the validateTime to make additional check that the number of milliseconds is in range 0..999.

    Methods inherited from class com.oss.asn1.AbstractData

    delete, equals, isEncodable, isPDU, isValid, toString, toString

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • GeneralizedTime

      public GeneralizedTime()
      The default constructor.
    • GeneralizedTime

      public GeneralizedTime(int year, int month, int day, int hour, int minute, int second, int minDiff, int millisecond, boolean isUTCTime)
      Construct from individual time components.
      Parameters:
      year - the year to set.
      month - the month to set.
      day - the day to set.
      hour - the hour to set.
      minute - the minute to sey.
      second - the second to set.
      minDiff - the minute-differential to set.
      millisecond - the millisecond to set.
      isUTCTime - true if 'Z' is included.
    • GeneralizedTime

      public GeneralizedTime(int[] components, boolean isUTCTime)
      Construct an instance of this class from an int array. The int array contains the time components in the order: year, month, day, hour, minute, second, minute-differential and millisecond.
      Parameters:
      components - the int array of time components.
      isUTCTime - true if 'Z' is included.
    • GeneralizedTime

      public GeneralizedTime(String value) throws BadTimeFormatException
      Construct an instance of this class from a String containing value notation of the GeneralizedTime.
      Parameters:
      value - string containing GeneralizedTime value.
      Throws:
      BadTimeFormatException - if value has invalid format
      See Also:
  • Method Details

    • clone

      public GeneralizedTime clone()
      Clone 'this' object.
      Overrides:
      clone in class AbstractTime
      Returns:
      deep copy of 'this' object.
    • getMillisecond

      public final int getMillisecond()
      Get the millisecond component of 'this' object.
      Returns:
      an int indicating the millisecond.
    • setMillisecond

      public final void setMillisecond(int millisecond)
      Set the millisecond component of 'this' object.
      Parameters:
      millisecond - the millisecond to set.
    • getIsUTCTime

      public final boolean getIsUTCTime()
      Determine whether this GeneralizedTime represents a UTCTime value (with 'Z').
      Returns:
      boolean indicating the status as UTCTime.
    • setIsUTCTime

      public final void setIsUTCTime(boolean isUTCTime)
      Specify that this GeneralizedTime should represent UTCTime.
      Parameters:
      isUTCTime - true if UTCTime, false if GeneralizedTime.
    • setMinuteDifferential

      public final void setMinuteDifferential(int minDiff)
      Set the minute-differential component of 'this' object if the isUTCTime flag is false. Otherwise, do nothing.
      Overrides:
      setMinuteDifferential in class AbstractTime
      Parameters:
      minDiff - the minute-differential to set.
    • intArrayValue

      public final int[] intArrayValue()
      Get an int array containing the components of 'this' object. The components are in the order: year, month, day, hour, minute, second, minute-differential, millisecond.
      Overrides:
      intArrayValue in class AbstractTime
      Returns:
      int array of time components.
    • setValue

      public final void setValue(int[] components, boolean isUTCTime)
      Set the value of a GeneralizedTime from an array of int that contains the year, month, day, hour, minute, second, minute-differential and millisecond.
      Parameters:
      components - an array of time components.
      isUTCTime - a boolean specified whether this GeneralizedTime represents UTCTime with 'Z'.
    • setValue

      public void setValue(String value) throws BadTimeFormatException
      Set an instance of this class from a String containing value notation of the GeneralizedTime. The value notation has the following format:
                 YYYYMMDDHHMMSS[.FFF]+hhmm
             or
                 YYYYMMDDHHMMSS[.FFF]-hhmm
             or
                 YYYYMMDDHHMMSS[.FFF]
             or
                 YYYYMMDDHHMMSS[.FFF]Z
             
      where YYYY is a four-digit year, and MM, DD, HH, MM, SS and FFF mean month, day, hour, minute, second and fractions of second (millisecond) correspondingly. If millisecond equal to zero, the fractional part (including the decimal point) is omitted. First two forms used to represent values with non-zero minute differential. Third form means that the time is a local time and the fourth form used to represent UTC time.
      Parameters:
      value - string containing GeneralizedTime value.
      Throws:
      BadTimeFormatException - if value has invalid format
    • toDate

      public final Date toDate(Calendar calendar)
      Get a Java Date object primed with the data from 'this' AbstractTime object using the calendar "calendar". Note that the precision is milliseconds.
      Overrides:
      toDate in class AbstractTime
      Parameters:
      calendar - the Calendar to use to compute the Date.
      Returns:
      reference to the Java Date set to 'this' object's year, month, day, hour, minute and second by using "calendar".
    • equalTo

      public final boolean equalTo(GeneralizedTime that)
      Compare 'this' object to another GeneralizedTime object to see if their contents are the same.
      Parameters:
      that - the GeneralizedTime object to compare 'this' object to.
      Returns:
      true if contents of both objects are the same.
    • compareTo

      public final int compareTo(GeneralizedTime that)
      Compare 'this' object to another GeneralizedTime object to determine whether the contents of this object is less than, equal to, or greater than the contents of other object.
      Parameters:
      that - the GeneralizedTime object to compare 'this' object to.
      Returns:
      LESS_THAN, EQUALS, GREATER_THAN values as the results of comparison.
      Throws:
      NullPointerException - thrown when 'that' is null.
    • compareTo

      public final int compareTo(GeneralizedTime that, TimeZone zone, Locale locale)
      Compare 'this' object to another object of the same class to determine whether the contents of this object is less than, equal to, or greater than the contents of other object.
      Parameters:
      that - the GeneralizedTime object to compare 'this' object to.
      zone - the TimeZone object to use during comparison.
      locale - the Locale object to use during comparison.
      Returns:
      LESS_THAN, EQUALS, GREATER_THAN values as the results of comparison.
      Throws:
      NullPointerException - thrown when 'that' is null.
    • formatTime

      public final String formatTime() throws com.oss.util.BadTimeValueException
      Takes the instance of AbstractTime and converts it to the formatted string, suitable for the encoding. The string returned has the following format:
              YYYYMMDDHHMMSS[.FFF]+hhmm
          or
              YYYYMMDDHHMMSS[.FFF]-hhmm
          or
              YYYYMMDDHHMMSS[.FFF]
          or
              YYYYMMDDHHMMSS[.FFF]Z
          
      where YYYY is a four-digit year, and MM, DD, HH, MM, SS and FFF mean month, day, hour, minute, second and fractions of second (millisecond) correspondingly. If millisecond equal to zero, the fractional part (including the decimal point) is omitted. First two forms used to represent values with non-zero minute differential. Third form means that the time is a local time and the fourth form used to represent UTC time. The method implements the following logic to select the format:
      1. if the time value has the UTC flag set (isUTCTime() method returns true) then the fourth form is used (UTC time),
      2. otherwise, if the value of minute differential is zero (getMinuteDifferential() method returns 0) then the third form is used (local time),
      3. otherwise, the first or the second form is used, depending on the sign of the minute differential.
      Specified by:
      formatTime in class AbstractTime
      Returns:
      string that contain the value, formatted accordingly to the GeneralizedTime format
      Throws:
      com.oss.util.BadTimeValueException - if the the value contains invalid components that make the formatting impossible
    • parseTime

      public AbstractTime parseTime(String str) throws BadTimeFormatException
      Takes formatted string that contains the value of GeneralizedTime, splits the string to the components and initializes the instance of the AbstractTime class, passed as a parameter. If the string has invalid format, a BadTimeFormatException exception is thrown. The method expects that the string has the format, specified in the description of the formatTime method. Additionally, the parser recognizes the following forms for the 'time of day' (the HHMMSS.FFF portion), since these forms are allowed by the ISO 8601:
      1. .FFF - fractions of day
      2. HH.FFF - fractions of hour
      3. HHMM.FFF - fractions of minute
      4. HHMMSS.FFF - fractions of second
      Where the fractional part .FFF may contain arbitrary number of digits (not necessarily three). The method always converts the fractional part to the milliseconds. To avoid unnecessary floating point arithmetic, it implements 'manual' rounding. On the first stage, the rounding procedure determines the number of significant digits in the fractional part (or the precision). For example, one day contains 86400000 milliseconds. So it make sense to truncate fractional part to seven decimal digits. One hour contains 3600000 milliseconds - in this case we need only six digits from the fractional part, etc.

      Let N is the precision chosen. If the fractional part contains more than N+1 digits, extra digits are ignored and the fractional part is recomputed to the number of milliseconds using the following formulas. Here f is an integer number formed by the first N+1 digits of the fractional part:

      1. fractions of day (1 day is equal to 86400000 msec).
        
                N = 7
                msec = 86400000*f/100000000 + 0.5 = 864*f/1000 + 0.5 =
                    4f/5 + 8f/125 + 0.5 = 4f DIV 5 + 8f DIV 125 +
                        (25*(4f MOD 5) + (8f MOD 125) + 62)/125
            
      2. fractions of hour (1 hour is equal to 3600000 msec).
        
                N = 6
                msec = 3600000*f/10000000 + 0.5 = 36*f/100 + 0.5 =
                    (9f + 12)/25
            
      3. fractions of minute (1 minute is equal to 60000 msec).
        
                N = 4
                msec = 60000*f/100000 + 0.5 = 6*f/10 + 0.5 =
                    (3f + 2)/5
            
      4. fractions of second (1 second is equal to 1000 msec).
        
                N = 3
                msec = 1000*f/1000 + 0.5 = (f + 5)/10
            
      Note: one extra digit is taken into account for better rounding. Otherwise, the number f is normalized to have N digits (by multiplying it by 10) and the number of milliseconds is computed as follows:
      1. fractions of day (1 day is equal to 86400000 msec).
        
                N = 7
                msec = 86400000*f/10000000 + 0.5 = 864*f/100 + 0.5 =
                    8f + (16f + 12)/25
            
      2. fractions of hour (1 hour is equal to 3600000 msec).
        
                N = 6
                msec = 3600000*f/1000000 + 0.5 = 36*f/10 + 0.5 =
                    (18f + 2)/5
            
      3. fractions of minute (1 minute is equal to 60000 msec).
        
                N = 4
                msec = 60000*f/10000 + 0.5 = 6*f
            
      4. fractions of second (1 second is equal to 1000 msec).
        
                N = 3
                msec = f
            
      Specified by:
      parseTime in class AbstractTime
      Parameters:
      str - formatted string that contains the value of UTCTime
      Returns:
      reference to initialized object
      Throws:
      BadTimeFormatException - if str has invalid format
    • validateTime

      public boolean validateTime() throws com.oss.util.BadTimeValueException
      Overrides basic version of the validateTime to make additional check that the number of milliseconds is in range 0..999.
      Overrides:
      validateTime in class AbstractTime
      Returns:
      true if value passed validation check
      Throws:
      com.oss.util.BadTimeValueException - if any component has an illegal value.
    • getTypeName

      public String getTypeName()
      Returns the name of ASN.1 type this java class represents.
      Overrides:
      getTypeName in class AbstractData
      Returns:
      the name of the ASN.1 type.