1 package org.bouncycastle.asn1;
9 * Generalized time object.
11 public class DERGeneralizedTime
17 * return a generalized time from the passed in object
19 * @exception IllegalArgumentException if the object cannot be converted.
21 public static DERGeneralizedTime getInstance(
24 if (obj == null || obj instanceof DERGeneralizedTime)
26 return (DERGeneralizedTime)obj;
29 if (obj instanceof ASN1OctetString)
31 return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
34 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
38 * return a Generalized Time object from a tagged object.
40 * @param obj the tagged object holding the object we want
41 * @param explicit true if the object is meant to be explicitly
42 * tagged false otherwise.
43 * @exception IllegalArgumentException if the tagged object cannot
46 public static DERGeneralizedTime getInstance(
50 return getInstance(obj.getObject());
54 * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
55 * for local time, or Z+-HHMM on the end, for difference between local
59 * @param time the time string.
61 public DERGeneralizedTime(
68 * base constructer from a java.util.date object
70 public DERGeneralizedTime(
73 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
75 dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
77 this.time = dateF.format(time);
84 // explicitly convert to characters
86 char[] dateC = new char[bytes.length];
88 for (int i = 0; i != dateC.length; i++)
90 dateC[i] = (char)(bytes[i] & 0xff);
93 this.time = new String(dateC);
97 * return the time - always in the form of
98 * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
100 * Normally in a certificate we would expect "Z" rather than "GMT",
101 * however adding the "GMT" means we can just use:
103 * dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
105 * To read in the time and get a date which is compatible with our local
108 public String getTime()
111 // standardise the format.
113 if (time.length() == 15)
115 return time.substring(0, 14) + "GMT+00:00";
117 else if (time.length() == 17)
119 return time.substring(0, 14) + "GMT" + time.substring(15, 17) + ":" + time.substring(17, 19);
125 private byte[] getOctets()
127 char[] cs = time.toCharArray();
128 byte[] bs = new byte[cs.length];
130 for (int i = 0; i != cs.length; i++)
143 out.writeEncoded(GENERALIZED_TIME, this.getOctets());
146 public boolean equals(
149 if ((o == null) || !(o instanceof DERGeneralizedTime))
154 return time.equals(((DERGeneralizedTime)o).time);