KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERGeneralizedTime


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.util.SimpleTimeZone;
8
9 /**
10  * Generalized time object.
11  */

12 public class DERGeneralizedTime
13     extends DERObject
14 {
15     String time;
16
17     /**
18      * return a generalized time from the passed in object
19      *
20      * @exception IllegalArgumentException if the object cannot be converted.
21      */

22     public static DERGeneralizedTime getInstance(
23         Object obj)
24     {
25         if (obj == null || obj instanceof DERGeneralizedTime)
26         {
27             return (DERGeneralizedTime)obj;
28         }
29
30         if (obj instanceof ASN1OctetString)
31         {
32             return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
33         }
34
35         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
36     }
37
38     /**
39      * return a Generalized Time object from a tagged object.
40      *
41      * @param obj the tagged object holding the object we want
42      * @param explicit true if the object is meant to be explicitly
43      * tagged false otherwise.
44      * @exception IllegalArgumentException if the tagged object cannot
45      * be converted.
46      */

47     public static DERGeneralizedTime getInstance(
48         ASN1TaggedObject obj,
49         boolean explicit)
50     {
51         return getInstance(obj.getObject());
52     }
53     
54     /**
55      * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
56      * for local time, or Z+-HHMM on the end, for difference between local
57      * time and UTC time.
58      * <p>
59      *
60      * @param time the time string.
61      */

62     public DERGeneralizedTime(
63         String time)
64     {
65         this.time = time;
66     }
67
68     /**
69      * base constructer from a java.util.date object
70      */

71     public DERGeneralizedTime(
72         Date time)
73     {
74         SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
75
76         dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
77
78         this.time = dateF.format(time);
79     }
80
81     DERGeneralizedTime(
82         byte[] bytes)
83     {
84         //
85
// explicitly convert to characters
86
//
87
char[] dateC = new char[bytes.length];
88
89         for (int i = 0; i != dateC.length; i++)
90         {
91             dateC[i] = (char)(bytes[i] & 0xff);
92         }
93
94         this.time = new String(dateC);
95     }
96
97     /**
98      * return the time - always in the form of
99      * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
100      * <p>
101      * Normally in a certificate we would expect "Z" rather than "GMT",
102      * however adding the "GMT" means we can just use:
103      * <pre>
104      * dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
105      * </pre>
106      * To read in the time and get a date which is compatible with our local
107      * time zone.
108      */

109     public String getTime()
110     {
111         //
112
// standardise the format.
113
//
114
if (time.charAt(time.length() - 1) == 'Z')
115         {
116             return time.substring(0, time.length() - 1) + "GMT+00:00";
117         }
118         else
119         {
120             int signPos = time.length() - 5;
121             char sign = time.charAt(signPos);
122             if (sign == '-' || sign == '+')
123             {
124                 return time.substring(0, signPos)
125                     + "GMT"
126                     + time.substring(signPos, signPos + 3)
127                     + ":"
128                     + time.substring(signPos + 3);
129             }
130             else
131             {
132                 signPos = time.length() - 3;
133                 sign = time.charAt(signPos);
134                 if (sign == '-' || sign == '+')
135                 {
136                     return time.substring(0, signPos)
137                         + "GMT"
138                         + time.substring(signPos)
139                         + ":00";
140                 }
141             }
142         }
143
144         return time;
145     }
146
147     public Date getDate()
148         throws ParseException
149     {
150         SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
151         
152         dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
153         
154         return dateF.parse(time);
155     }
156     
157     private byte[] getOctets()
158     {
159         char[] cs = time.toCharArray();
160         byte[] bs = new byte[cs.length];
161
162         for (int i = 0; i != cs.length; i++)
163         {
164             bs[i] = (byte)cs[i];
165         }
166
167         return bs;
168     }
169
170
171     void encode(
172         DEROutputStream out)
173         throws IOException
174     {
175         out.writeEncoded(GENERALIZED_TIME, this.getOctets());
176     }
177     
178     public boolean equals(
179         Object o)
180     {
181         if ((o == null) || !(o instanceof DERGeneralizedTime))
182         {
183             return false;
184         }
185
186         return time.equals(((DERGeneralizedTime)o).time);
187     }
188 }
189
Popular Tags