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     * Private constructor.
038     */
039    private DateUtils()
040    {
041    }
042
043
044    /**
045     * Return a Date instance from a String 
046     *
047     * @param zuluTime The String to convert
048     * @return The Date instance
049     */
050    public static Date getDate( String zuluTime )
051    {
052        try
053        {
054            return GeneralizedTime.getDate( zuluTime );
055        }
056        catch ( Exception e )
057        {
058            throw new RuntimeException( e );
059        }
060    }
061
062
063    /**
064     * Gets the generalized time right now. {@link GeneralizedTime}
065     * 
066     * @return the generalizedTime right now
067     */
068    public static String getGeneralizedTime()
069    {
070        return new GeneralizedTime( new Date() ).toGeneralizedTime();
071    }
072
073
074    /**
075     * 
076     * @see #getGeneralizedTime()
077     *
078     * @param date the date to be converted to generalized time string
079     * @return given date in the generalized time string format
080     */
081    public static String getGeneralizedTime( Date date )
082    {
083        return new GeneralizedTime( date ).toGeneralizedTime();
084    }
085
086
087    /**
088     * 
089     * @see #getGeneralizedTime()
090     *
091     * @param time the time value to be converted to generalized time string
092     * @return given time in generalized time string format
093     */
094    public static String getGeneralizedTime( long time )
095    {
096        return getGeneralizedTime( new Date( time ) );
097    }
098
099
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}