KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > Time


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1.x509;
21
22 import java.text.ParsePosition JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.SimpleTimeZone JavaDoc;
26
27 import com.maverick.crypto.asn1.ASN1TaggedObject;
28 import com.maverick.crypto.asn1.DEREncodable;
29 import com.maverick.crypto.asn1.DERGeneralizedTime;
30 import com.maverick.crypto.asn1.DERObject;
31 import com.maverick.crypto.asn1.DERUTCTime;
32
33 public class Time
34     implements DEREncodable
35 {
36     DERObject time;
37
38     public static Time getInstance(
39         ASN1TaggedObject obj,
40         boolean explicit)
41     {
42         return getInstance(obj.getObject());
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 getDERObject()
130     {
131         return time;
132     }
133 }
134
Popular Tags