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 */ 020package org.apache.directory.api.util; 021 022 023import java.text.ParseException; 024import java.util.Date; 025 026import org.apache.directory.api.i18n.I18n; 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 */ 034public final class DateUtils 035{ 036 037 /** 038 * Private constructor. 039 */ 040 private DateUtils() 041 { 042 } 043 044 045 /** 046 * Return a Date instance from a String 047 * 048 * @param zuluTime The String to convert 049 * @return The Date instance 050 */ 051 public static Date getDate( String zuluTime ) 052 { 053 try 054 { 055 return GeneralizedTime.getDate( zuluTime ); 056 } 057 catch ( Exception e ) 058 { 059 throw new RuntimeException( e ); 060 } 061 } 062 063 064 /** 065 * Gets the generalized time right now. {@link GeneralizedTime} 066 * 067 * @return the generalizedTime right now 068 */ 069 public static String getGeneralizedTime() 070 { 071 return new GeneralizedTime( new Date() ).toGeneralizedTime(); 072 } 073 074 075 /** 076 * 077 * @see #getGeneralizedTime() 078 * 079 * @param date the date to be converted to generalized time string 080 * @return given date in the generalized time string format 081 */ 082 public static String getGeneralizedTime( Date date ) 083 { 084 return new GeneralizedTime( date ).toGeneralizedTime(); 085 } 086 087 088 /** 089 * 090 * @see #getGeneralizedTime() 091 * 092 * @param time the time value to be converted to generalized time string 093 * @return given time in generalized time string format 094 */ 095 public static String getGeneralizedTime( long time ) 096 { 097 return getGeneralizedTime( new Date( time ) ); 098 } 099 100 101 /** 102 * Converts the 18-digit Active Directory timestamps, also named 'Windows NT time format' or 'Win32 FILETIME or SYSTEMTIME'. 103 * These are used in Microsoft Active Directory for pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp and LastPwdSet. 104 * The timestamp is the number of 100-nanoseconds intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC. 105 * <p> 106 * 107 * @param intervalDate 18-digit number. Time in 100-nanoseconds intervals since 1.1.1601 108 * @return The converted date 109 * @throws ParseException If the given interval is not valid 110 */ 111 public static Date convertIntervalDate( String intervalDate ) throws ParseException 112 { 113 if ( intervalDate == null ) 114 { 115 throw new ParseException( I18n.err( I18n.ERR_17043_GENERALIZED_TIME_NULL ), 0 ); 116 } 117 118 long offset = 11644473600000L; // offset milliseconds from Jan 1, 1601 to Jan 1, 1970 119 120 // convert 100-nanosecond intervals to milliseconds (10000 = 1 000 000ns / 100) 121 long javaTime = Long.parseLong( intervalDate ) / 10000L - offset; 122 123 return new Date( javaTime ); 124 } 125}