1 package org.bouncycastle.asn1;
10 public class DERUTCTime
16 * return an UTC Time from the passed in object.
18 * @exception IllegalArgumentException if the object cannot be converted.
20 public static DERUTCTime getInstance(
23 if (obj == null || obj instanceof DERUTCTime)
25 return (DERUTCTime)obj;
28 if (obj instanceof ASN1OctetString)
30 return new DERUTCTime(((ASN1OctetString)obj).getOctets());
33 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
37 * return an UTC Time 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 DERUTCTime getInstance(
49 return getInstance(obj.getObject());
53 * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
54 * never encoded. When you're creating one of these objects from scratch, that's
55 * what you want to use, otherwise we'll try to deal with whatever gets read from
56 * the input stream... (this is why the input format is different from the getTime()
60 * @param time the time string.
69 * base constructer from a java.util.date object
74 SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
76 dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
78 this.time = dateF.format(time);
85 // explicitly convert to characters
87 char[] dateC = new char[bytes.length];
89 for (int i = 0; i != dateC.length; i++)
91 dateC[i] = (char)(bytes[i] & 0xff);
94 this.time = new String(dateC);
98 * return the time - always in the form of
99 * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
101 * Normally in a certificate we would expect "Z" rather than "GMT",
102 * however adding the "GMT" means we can just use:
104 * dateF = new SimpleDateFormat("yyMMddHHmmssz");
106 * To read in the time and get a date which is compatible with our local
109 * <b>Note:</b> In some cases, due to the local date processing, this
110 * may lead to unexpected results. If you want to stick the normal
111 * convention of 1950 to 2049 use the getAdjustedTime() method.
113 public String getTime()
116 // standardise the format.
118 if (time.length() == 11)
120 return time.substring(0, 10) + "00GMT+00:00";
122 else if (time.length() == 13)
124 return time.substring(0, 12) + "GMT+00:00";
126 else if (time.length() == 17)
128 return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
135 * return the time as an adjusted date with a 4 digit year. This goes
136 * in the range of 1950 - 2049.
138 public String getAdjustedTime()
140 String d = this.getTime();
142 if (d.charAt(0) < '5')
152 private byte[] getOctets()
154 char[] cs = time.toCharArray();
155 byte[] bs = new byte[cs.length];
157 for (int i = 0; i != cs.length; i++)
169 out.writeEncoded(UTC_TIME, this.getOctets());
172 public boolean equals(
175 if ((o == null) || !(o instanceof DERUTCTime))
180 return time.equals(((DERUTCTime)o).time);