001    /*
002     * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003     * 
004     * Permission is hereby granted, free of charge, to any person obtaining a copy of this
005     * software and associated documentation files (the "Software"), to deal in the Software
006     * without restriction, including without limitation the rights to use, copy, modify,
007     * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
008     * permit persons to whom the Software is furnished to do so, subject to the following
009     * conditions:
010     * 
011     * The above copyright notice and this permission notice shall be included in all copies
012     * or substantial portions of the Software.
013     * 
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
015     * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
016     * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
017     * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018     * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
019     * DEALINGS IN THE SOFTWARE.
020     * 
021     */
022    
023    package org.apache.directory.shared.asn1.der;
024    
025    
026    import java.text.ParseException;
027    import java.text.SimpleDateFormat;
028    import java.util.Date;
029    import java.util.TimeZone;
030    
031    
032    /**
033     * DER UTC time object.
034     */
035    public class DERUTCTime extends DERString
036    {
037        private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
038    
039        private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyMMddHHmmss'Z'" );
040    
041        static
042        {
043            dateFormat.setTimeZone( UTC_TIME_ZONE );
044        }
045    
046    
047        /**
048         * Basic DERObject constructor.
049         */
050        public DERUTCTime(byte[] value)
051        {
052            super( UTC_TIME, value );
053        }
054    
055    
056        /**
057         * Static factory method, type-conversion operator.
058         */
059        public static DERUTCTime valueOf( Date date )
060        {
061            String dateString = null;
062    
063            synchronized ( dateFormat )
064            {
065                dateString = dateFormat.format( date );
066            }
067    
068            byte[] bytes = stringToByteArray( dateString );
069    
070            return new DERUTCTime( bytes );
071        }
072    
073    
074        /**
075         * Lazy accessor
076         * 
077         * @return Date representation of this DER UTC Time
078         * @throws ParseException
079         */
080        public Date getDate() throws ParseException
081        {
082            String string = byteArrayToString( value );
083    
084            synchronized ( dateFormat )
085            {
086                return dateFormat.parse( string );
087            }
088        }
089    }