View Javadoc

1    /*
2    *   Copyright 2005 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
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