KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > der > DERUTCTime


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.der;
9
10 import org.enhydra.oyster.exception.SMIMEException;
11 import org.enhydra.oyster.exception.ErrorStorage;
12 import java.util.GregorianCalendar JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.TimeZone JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16
17 /**
18  * DERUTCTime is primitive type of DER encoded object which represents Coordinate
19  * Universal Time (or Greenwich Mean Time) in the ASN.1 notation in format
20  * YYMMDDhhmmssZ. For example: 01.02.1971. 11:25:30 GMT+1h take a form in
21  * DERUTCTime object: 710201102530Z.
22  */

23 public class DERUTCTime extends DERObject {
24
25 /**
26  * Constructs UTC time according to user's current local settings (increased
27  * or decreased for time difference to obtain Greenwich Mean Time).
28  * @exception SMIMEException thrown in super class constructor. Also it can be
29  * caused by non SMIMEException which is: UnsupportedEncodingException.
30  */

31   public DERUTCTime () throws SMIMEException
32   {
33     super(23);
34     TimeZone JavaDoc tz = TimeZone.getDefault();
35     tz.setRawOffset(0); // Setting of GMT 0 offset
36
GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(tz);
37     SimpleDateFormat JavaDoc datForm = new SimpleDateFormat JavaDoc("yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
38
datForm.setCalendar(cal);
39     byte[] utcTime = null;
40     try {
41       utcTime = datForm.format(cal.getTime()).getBytes("ISO-8859-1");
42     }
43     catch(Exception JavaDoc e) {
44       throw SMIMEException.getInstance(this, e, "constructor" );
45     }
46     this.addContent(utcTime);
47   }
48
49 /**
50  * Constructs UTC time according to the submited information in Date class
51  * @param dat0 information about date and time.
52  * @exception SMIMEException thrown in super class constructor. Also, it can be
53  * caused by non SMIMEException which is: UnsupportedEncodingException.
54  */

55   public DERUTCTime (Date JavaDoc dat0) throws SMIMEException
56   {
57     super(23);
58     TimeZone JavaDoc tz = TimeZone.getDefault();
59     tz.setRawOffset(0); // Setting of GMT 0 offset
60
GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(tz);
61     cal.setTime(dat0);
62     SimpleDateFormat JavaDoc datForm = new SimpleDateFormat JavaDoc("yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
63
datForm.setCalendar(cal);
64     byte[] utcTime = null;
65     try {
66       utcTime = datForm.format(cal.getTime()).getBytes("ISO-8859-1");
67     }
68     catch(Exception JavaDoc e) {
69       throw SMIMEException.getInstance(this, e, "constructor" );
70     }
71     this.addContent(utcTime);
72   }
73
74 /**
75  * Constructs UTC time according to submited information in GregorianCalendar
76  * class.
77  * @param cal0 information about date and time.
78  * @exception SMIMEException thrown in super class constructor. Also, it can be
79  * caused by non SMIMEException which is: UnsupportedEncodingException.
80  */

81   public DERUTCTime (GregorianCalendar JavaDoc cal0) throws SMIMEException
82   {
83     super(23);
84     TimeZone JavaDoc tz = TimeZone.getDefault();
85     tz.setRawOffset(0); // Setting of GMT 0 offset
86
cal0.setTimeZone(tz);
87     SimpleDateFormat JavaDoc datForm = new SimpleDateFormat JavaDoc("yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
88
datForm.setCalendar(cal0);
89     byte[] utcTime = null;
90     try {
91       utcTime = datForm.format(cal0.getTime()).getBytes("ISO-8859-1");
92     }
93     catch(Exception JavaDoc e) {
94       throw SMIMEException.getInstance(this, e, "constructor" );
95     }
96     this.addContent(utcTime);
97   }
98
99 /**
100  * Constructs UTC time according to the definition of elements in byte array
101  * it the following form: YYMMDDhhmmssZ
102  * @param utcTime0 byte array representation of UTC elements.
103  * @exception SMIMEException thrown in super class constructor.
104  */

105   public DERUTCTime (byte[] utcTime0) throws SMIMEException
106   {
107     super(23);
108     this.addContent(utcTime0);
109   }
110
111 /**
112  * Constructs UTC time according to the definition of elements in String
113  * it the following form: YYMMDDhhmmssZ
114  * @param utcTime0 is String representation of UTC elements.
115  * @exception SMIMEException thrown in super class constructor. Also, it can be
116  * caused by non SMIMEException which is: UnsupportedEncodingException.
117  */

118   public DERUTCTime (String JavaDoc utcTime0) throws SMIMEException
119   {
120     super(23);
121     try {
122       this.addContent(utcTime0.getBytes("ISO-8859-1"));
123     }
124     catch(Exception JavaDoc e) {
125       throw SMIMEException.getInstance(this, e, "constructor" );
126     }
127   }
128 }
129
130
131
132
Popular Tags