1 package org.bouncycastle.asn1;
11 public class DERUTCTime
17 * return an UTC Time from the passed in object.
19 * @exception IllegalArgumentException if the object cannot be converted.
21 public static DERUTCTime getInstance(
24 if (obj == null || obj instanceof DERUTCTime)
26 return (DERUTCTime)obj;
29 if (obj instanceof ASN1OctetString)
31 return new DERUTCTime(((ASN1OctetString)obj).getOctets());
34 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
38 * return an UTC Time 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 DERUTCTime getInstance(
50 return getInstance(obj.getObject());
54 * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
55 * never encoded. When you're creating one of these objects from scratch, that's
56 * what you want to use, otherwise we'll try to deal with whatever gets read from
57 * the input stream... (this is why the input format is different from the getTime()
61 * @param time the time string.
70 * base constructer from a java.util.date object
75 SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
77 dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
79 this.time = dateF.format(time);
86 // explicitly convert to characters
88 char[] dateC = new char[bytes.length];
90 for (int i = 0; i != dateC.length; i++)
92 dateC[i] = (char)(bytes[i] & 0xff);
95 this.time = new String(dateC);
99 * return the time - always in the form of
100 * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
102 * Normally in a certificate we would expect "Z" rather than "GMT",
103 * however adding the "GMT" means we can just use:
105 * dateF = new SimpleDateFormat("yyMMddHHmmssz");
107 * To read in the time and get a date which is compatible with our local
110 * <b>Note:</b> In some cases, due to the local date processing, this
111 * may lead to unexpected results. If you want to stick the normal
112 * convention of 1950 to 2049 use the getAdjustedTime() method.
114 public String getTime()
117 // standardise the format.
119 if (time.length() == 11)
121 return time.substring(0, 10) + "00GMT+00:00";
123 else if (time.length() == 13)
125 return time.substring(0, 12) + "GMT+00:00";
127 else if (time.length() == 17)
129 return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
136 * return the time as an adjusted date with a 4 digit year. This goes
137 * in the range of 1950 - 2049.
139 public String getAdjustedTime()
141 String d = this.getTime();
143 if (d.charAt(0) < '5')
153 private byte[] getOctets()
155 char[] cs = time.toCharArray();
156 byte[] bs = new byte[cs.length];
158 for (int i = 0; i != cs.length; i++)
170 out.writeEncoded(UTC_TIME, this.getOctets());
173 public boolean equals(
176 if ((o == null) || !(o instanceof DERUTCTime))
181 return time.equals(((DERUTCTime)o).time);