001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.util;
021    
022    
023    import java.text.SimpleDateFormat;
024    import java.util.Calendar;
025    import java.util.Date;
026    import java.util.TimeZone;
027    
028    
029    /**
030     * Gets the generalized time using the "Z" form of the g-time-zone.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev: 725712 $
034     */
035    public class DateUtils
036    {
037        private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
038    
039        private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss'Z'" );
040    
041        static
042        {
043            dateFormat.setTimeZone( UTC_TIME_ZONE );
044        }
045        
046        
047        public static Date getDate( String zuluTime )
048        {
049            Calendar cal = Calendar.getInstance( UTC_TIME_ZONE );
050            cal.set( Calendar.YEAR, getYear( zuluTime ) ); 
051            cal.set( Calendar.MONTH, getMonth( zuluTime ) - 1 ); 
052            cal.set( Calendar.DAY_OF_MONTH, getDay( zuluTime ) ); 
053            cal.set( Calendar.HOUR_OF_DAY, getHour( zuluTime ) ); 
054            cal.set( Calendar.MINUTE, getMinutes( zuluTime ) );
055            cal.set( Calendar.SECOND, getSeconds( zuluTime ) );
056            return cal.getTime();
057        }
058    
059    
060        public static int getYear( String zuluTime )
061        {
062            return Integer.parseInt( zuluTime.substring( 0, 4 ) );
063        }
064        
065        
066        public static int getMonth( String zuluTime )
067        {
068            return Integer.parseInt( zuluTime.substring( 4, 6 ) );
069        }
070        
071        
072        public static int getDay( String zuluTime )
073        {
074            return Integer.parseInt( zuluTime.substring( 6, 8 ) );
075        }
076        
077        
078        public static int getHour( String zuluTime )
079        {
080            return Integer.parseInt( zuluTime.substring( 8, 10 ) );
081        }
082        
083        
084        public static int getMinutes( String zuluTime )
085        {
086            return Integer.parseInt( zuluTime.substring( 10, 12 ) );
087        }
088        
089        
090        public static int getSeconds( String zuluTime )
091        {
092            return Integer.parseInt( zuluTime.substring( 12, 14 ) );
093        }
094        
095        
096        /**
097         * Gets the generalized time using the "Z" form of the g-time-zone described
098         * by [<a href=
099         * "http://ietf.org/internet-drafts/draft-ietf-ldapbis-syntaxes-09.txt">
100         * SYNTAXES</a>] section 3.3.13, included below:
101         * 
102         * <pre>
103         * 
104         *  3.3.13.  Generalized Time
105         * 
106         *  A value of the Generalized Time syntax is a character string
107         *  representing a date and time.  The LDAP-specific encoding of a value
108         *  of this syntax is a restriction of the format defined in [ISO8601],
109         *  and is described by the following ABNF:
110         * 
111         *  century = 2(%x30-39) ; &quot;00&quot; to &quot;99&quot;
112         *  year    = 2(%x30-39) ; &quot;00&quot; to &quot;99&quot;
113         *  month   =   ( %x30 %x31-39 ) ; &quot;01&quot; (January) to &quot;09&quot;
114         *            / ( %x31 %x30-32 ) ; &quot;10&quot; to &quot;12&quot;
115         *  day     =   ( %x30 %x31-39 )    ; &quot;01&quot; to &quot;09&quot;
116         *            / ( %x31-32 %x30-39 ) ; &quot;10&quot; to &quot;29&quot;
117         *            / ( %x33 %x30-31 )    ; &quot;30&quot; to &quot;31&quot;
118         *  hour    = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; &quot;00&quot; to &quot;23&quot;
119         *  minute  = %x30-35 %x30-39                        ; &quot;00&quot; to &quot;59&quot;
120         *  second  =   ( %x30-35 %x30-39 )  ; &quot;00&quot; to &quot;59&quot;
121         *            / ( %x36 %x30 )        ; &quot;60&quot; (a leap second)
122         * 
123         *  GeneralizedTime = century year month day hour
124         *                       [ minute [ second ] ] [ fraction ]
125         *                       g-time-zone
126         *  fraction        = ( DOT / COMMA ) 1*(%x30-39)
127         *  g-time-zone     = %x5A  ; &quot;Z&quot;
128         *                    / g-differential
129         *  g-differential  = ( MINUS / PLUS ) hour [ minute ]
130         *  MINUS           = %x2D  ; minus sign (&quot;-&quot;)
131         * 
132         *  The &lt;DOT&gt;, &lt;COMMA&gt; and &lt;PLUS&gt; rules are defined in [MODELS].
133         * 
134         *  The time value represents coordinated universal time (equivalent to
135         *  Greenwich Mean Time) if the &quot;Z&quot; form of &lt;g-time-zone&gt; is used,
136         * 
137         *  otherwise the value represents a local time in the time zone
138         *  indicated by &lt;g-differential&gt;.  In the latter case, coordinated
139         *  universal time can be calculated by subtracting the differential from
140         *  the local time.  The &quot;Z&quot; form of &lt;g-time-zone&gt; SHOULD be used in
141         *  preference to &lt;g-differential&gt;.
142         * 
143         *  Examples:
144         *     199412161032Z
145         *     199412160532-0500
146         * 
147         *  Both example values represent the same coordinated universal time:
148         *  10:32 AM, December 16, 1994.
149         * 
150         *  The LDAP definition for the Generalized Time syntax is:
151         * 
152         *  ( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )
153         * 
154         *  This syntax corresponds to the GeneralizedTime ASN.1 type from
155         *  [ASN.1], with the constraint that local time without a differential
156         *  SHALL NOT be used.
157         * </pre>
158         * 
159         * Gets the generalized time right now.
160         * 
161         * @return the generalizedTime right now
162         */
163        public static String getGeneralizedTime()
164        {
165            Date date = new Date();
166    
167            synchronized ( dateFormat )
168            {
169                return dateFormat.format( date );
170            }
171        }
172    
173    
174        /**
175         * 
176         * @see #getGeneralizedTime()
177         *
178         * @param date the date to be converted to generalized time string
179         * @return given date in the generalized time string format
180         */
181        public static String getGeneralizedTime( Date date )
182        {
183            synchronized ( dateFormat )
184            {
185                return dateFormat.format( date );
186            }
187        }
188    
189    
190        /**
191         * 
192         * @see #getGeneralizedTime()
193         *
194         * @param time the time value to be converted to generalized time string
195         * @return given time in generalized time string format
196         */
197        public static String getGeneralizedTime( long time )
198        {
199            return getGeneralizedTime( new Date( time ) );
200        }
201        
202    }