KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > Time


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 import java.text.ParsePosition JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.SimpleTimeZone JavaDoc;
24
25 import org.apache.geronimo.util.asn1.ASN1Choice;
26 import org.apache.geronimo.util.asn1.ASN1Encodable;
27 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
28 import org.apache.geronimo.util.asn1.DERGeneralizedTime;
29 import org.apache.geronimo.util.asn1.DERObject;
30 import org.apache.geronimo.util.asn1.DERUTCTime;
31
32 public class Time
33     extends ASN1Encodable
34     implements ASN1Choice
35 {
36     DERObject time;
37
38     public static Time getInstance(
39         ASN1TaggedObject obj,
40         boolean explicit)
41     {
42         return getInstance(obj.getObject()); // must be explicitly tagged
43
}
44
45     public Time(
46         DERObject time)
47     {
48         if (!(time instanceof DERUTCTime)
49             && !(time instanceof DERGeneralizedTime))
50         {
51             throw new IllegalArgumentException JavaDoc("unknown object passed to Time");
52         }
53
54         this.time = time;
55     }
56
57     /**
58      * creates a time object from a given date - if the date is between 1950
59      * and 2049 a UTCTime object is generated, otherwise a GeneralizedTime
60      * is used.
61      */

62     public Time(
63         Date JavaDoc date)
64     {
65         SimpleTimeZone JavaDoc tz = new SimpleTimeZone JavaDoc(0, "Z");
66         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
67
68         dateF.setTimeZone(tz);
69
70         String JavaDoc d = dateF.format(date) + "Z";
71         int year = Integer.parseInt(d.substring(0, 4));
72
73         if (year < 1950 || year > 2049)
74         {
75             time = new DERGeneralizedTime(d);
76         }
77         else
78         {
79             time = new DERUTCTime(d.substring(2));
80         }
81     }
82
83     public static Time getInstance(
84         Object JavaDoc obj)
85     {
86         if (obj instanceof Time)
87         {
88             return (Time)obj;
89         }
90         else if (obj instanceof DERUTCTime)
91         {
92             return new Time((DERUTCTime)obj);
93         }
94         else if (obj instanceof DERGeneralizedTime)
95         {
96             return new Time((DERGeneralizedTime)obj);
97         }
98
99         throw new IllegalArgumentException JavaDoc("unknown object in factory");
100     }
101
102     public String JavaDoc getTime()
103     {
104         if (time instanceof DERUTCTime)
105         {
106             return ((DERUTCTime)time).getAdjustedTime();
107         }
108         else
109         {
110             return ((DERGeneralizedTime)time).getTime();
111         }
112     }
113
114     public Date JavaDoc getDate()
115     {
116         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssz");
117
118         return dateF.parse(this.getTime(), new ParsePosition JavaDoc(0));
119     }
120
121     /**
122      * Produce an object suitable for an ASN1OutputStream.
123      * <pre>
124      * Time ::= CHOICE {
125      * utcTime UTCTime,
126      * generalTime GeneralizedTime }
127      * </pre>
128      */

129     public DERObject toASN1Object()
130     {
131         return time;
132     }
133 }
134
Popular Tags