1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.api.util; 21 22 23 import java.text.ParseException; 24 import java.util.Date; 25 26 import org.apache.directory.api.i18n.I18n; 27 28 29 /** 30 * Gets the generalized time using the "Z" form of the g-time-zone. 31 * 32 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 33 */ 34 public final class DateUtils 35 { 36 /** 37 * Private constructor. 38 */ 39 private DateUtils() 40 { 41 } 42 43 44 /** 45 * Return a Date instance from a String 46 * 47 * @param zuluTime The String to convert 48 * @return The Date instance 49 */ 50 public static Date getDate( String zuluTime ) 51 { 52 try 53 { 54 return GeneralizedTime.getDate( zuluTime ); 55 } 56 catch ( Exception e ) 57 { 58 throw new RuntimeException( e ); 59 } 60 } 61 62 63 /** 64 * Gets the generalized time right now. {@link GeneralizedTime} 65 * 66 * @return the generalizedTime right now 67 */ 68 public static String getGeneralizedTime() 69 { 70 return new GeneralizedTime( new Date() ).toGeneralizedTime(); 71 } 72 73 74 /** 75 * 76 * @see #getGeneralizedTime() 77 * 78 * @param date the date to be converted to generalized time string 79 * @return given date in the generalized time string format 80 */ 81 public static String getGeneralizedTime( Date date ) 82 { 83 return new GeneralizedTime( date ).toGeneralizedTime(); 84 } 85 86 87 /** 88 * 89 * @see #getGeneralizedTime() 90 * 91 * @param time the time value to be converted to generalized time string 92 * @return given time in generalized time string format 93 */ 94 public static String getGeneralizedTime( long time ) 95 { 96 return getGeneralizedTime( new Date( time ) ); 97 } 98 99 100 /** 101 * Converts the 18-digit Active Directory timestamps, also named 'Windows NT time format' or 'Win32 FILETIME or SYSTEMTIME'. 102 * These are used in Microsoft Active Directory for pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp and LastPwdSet. 103 * The timestamp is the number of 100-nanoseconds intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC. 104 * <p> 105 * 106 * @param intervalDate 18-digit number. Time in 100-nanoseconds intervals since 1.1.1601 107 * @return The converted date 108 * @throws ParseException If the given interval is not valid 109 */ 110 public static Date convertIntervalDate( String intervalDate ) throws ParseException 111 { 112 if ( intervalDate == null ) 113 { 114 throw new ParseException( I18n.err( I18n.ERR_04359 ), 0 ); 115 } 116 117 long offset = 11644473600000L; // offset milliseconds from Jan 1, 1601 to Jan 1, 1970 118 119 // convert 100-nanosecond intervals to milliseconds (10000 = 1 000 000ns / 100) 120 long javaTime = Long.parseLong( intervalDate ) / 10000L - offset; 121 122 return new Date( javaTime ); 123 } 124 }