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) ; "00" to "99" 112 * year = 2(%x30-39) ; "00" to "99" 113 * month = ( %x30 %x31-39 ) ; "01" (January) to "09" 114 * / ( %x31 %x30-32 ) ; "10" to "12" 115 * day = ( %x30 %x31-39 ) ; "01" to "09" 116 * / ( %x31-32 %x30-39 ) ; "10" to "29" 117 * / ( %x33 %x30-31 ) ; "30" to "31" 118 * hour = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23" 119 * minute = %x30-35 %x30-39 ; "00" to "59" 120 * second = ( %x30-35 %x30-39 ) ; "00" to "59" 121 * / ( %x36 %x30 ) ; "60" (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 ; "Z" 128 * / g-differential 129 * g-differential = ( MINUS / PLUS ) hour [ minute ] 130 * MINUS = %x2D ; minus sign ("-") 131 * 132 * The <DOT>, <COMMA> and <PLUS> rules are defined in [MODELS]. 133 * 134 * The time value represents coordinated universal time (equivalent to 135 * Greenwich Mean Time) if the "Z" form of <g-time-zone> is used, 136 * 137 * otherwise the value represents a local time in the time zone 138 * indicated by <g-differential>. In the latter case, coordinated 139 * universal time can be calculated by subtracting the differential from 140 * the local time. The "Z" form of <g-time-zone> SHOULD be used in 141 * preference to <g-differential>. 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 }