1 package org.bouncycastle.asn1;
8 * Generalized time object.
10 public class DERGeneralizedTime
16 * return a generalized time from the passed in object
18 * @exception IllegalArgumentException if the object cannot be converted.
20 public static DERGeneralizedTime getInstance(
23 if (obj == null || obj instanceof DERGeneralizedTime)
25 return (DERGeneralizedTime)obj;
28 if (obj instanceof ASN1OctetString)
30 return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
33 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
37 * return a Generalized Time object from a tagged object.
39 * @param obj the tagged object holding the object we want
40 * @param explicit true if the object is meant to be explicitly
41 * tagged false otherwise.
42 * @exception IllegalArgumentException if the tagged object cannot
45 public static DERGeneralizedTime getInstance(
49 return getInstance(obj.getObject());
53 * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
54 * for local time, or Z+-HHMM on the end, for difference between local
58 * @param time the time string.
60 public DERGeneralizedTime(
67 * base constructer from a java.util.date object
69 public DERGeneralizedTime(
72 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
74 dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
76 this.time = dateF.format(time);
83 // explicitly convert to characters
85 char[] dateC = new char[bytes.length];
87 for (int i = 0; i != dateC.length; i++)
89 dateC[i] = (char)(bytes[i] & 0xff);
92 this.time = new String(dateC);
96 * return the time - always in the form of
97 * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
99 * Normally in a certificate we would expect "Z" rather than "GMT",
100 * however adding the "GMT" means we can just use:
102 * dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
104 * To read in the time and get a date which is compatible with our local
107 public String getTime()
110 // standardise the format.
112 if (time.length() == 15)
114 return time.substring(0, 14) + "GMT+00:00";
116 else if (time.length() == 17)
118 return time.substring(0, 14) + "GMT" + time.substring(15, 17) + ":" + time.substring(17, 19);
124 private byte[] getOctets()
126 char[] cs = time.toCharArray();
127 byte[] bs = new byte[cs.length];
129 for (int i = 0; i != cs.length; i++)
142 out.writeEncoded(GENERALIZED_TIME, this.getOctets());
145 public boolean equals(
148 if ((o == null) || !(o instanceof DERGeneralizedTime))
153 return time.equals(((DERGeneralizedTime)o).time);