1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.asn1.der;
19
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.TimeZone;
24
25 /***
26 * DER Generalized time object.
27 */
28 public class DERGeneralizedTime extends DERString
29 {
30 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
31 private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss'Z'" );
32
33 static
34 {
35 dateFormat.setTimeZone( UTC_TIME_ZONE );
36 }
37
38 /***
39 * Basic DERObject constructor.
40 */
41 DERGeneralizedTime( byte[] value )
42 {
43 super( GENERALIZED_TIME, value );
44 }
45
46 /***
47 * Static factory method, type-conversion operator.
48 */
49 public static DERGeneralizedTime valueOf( Date date )
50 {
51 String dateString = null;
52
53 synchronized (dateFormat)
54 {
55 dateString = dateFormat.format( date );
56 }
57
58 byte[] bytes = stringToByteArray( dateString );
59
60 return new DERGeneralizedTime( bytes );
61 }
62
63 /***
64 * Lazy accessor
65 * @return Date representation of this DER Generalized Time
66 * @throws ParseException
67 */
68 public Date getDate()
69 throws ParseException
70 {
71 String string = byteArrayToString( value );
72
73
74 synchronized (dateFormat)
75 {
76 return dateFormat.parse( string );
77 }
78 }
79 }
80