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.util.Calendar;
024import java.util.Date;
025
026
027/**
028 * Gets the generalized time using the "Z" form of the g-time-zone.
029 * 
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 */
032public final class DateUtils
033{
034
035    /**
036     * Private constructor.
037     */
038    private DateUtils()
039    {
040    }
041
042
043    public static Date getDate( String zuluTime )
044    {
045        try
046        {
047            return GeneralizedTime.getDate( zuluTime );
048        }
049        catch( Exception e )
050        {
051            throw new RuntimeException( e );
052        }
053    }
054
055
056    /**
057     * Gets the generalized time right now. {@link GeneralizedTime}
058     * 
059     * @return the generalizedTime right now
060     */
061    public static String getGeneralizedTime()
062    {
063        return new GeneralizedTime( Calendar.getInstance() ).toGeneralizedTime();
064    }
065
066
067    /**
068     * 
069     * @see #getGeneralizedTime()
070     *
071     * @param date the date to be converted to generalized time string
072     * @return given date in the generalized time string format
073     */
074    public static String getGeneralizedTime( Date date )
075    {
076        Calendar calendar = Calendar.getInstance();
077        calendar.setTime( date );
078        return new GeneralizedTime( calendar ).toGeneralizedTime();
079    }
080
081
082    /**
083     * 
084     * @see #getGeneralizedTime()
085     *
086     * @param time the time value to be converted to generalized time string
087     * @return given time in generalized time string format
088     */
089    public static String getGeneralizedTime( long time )
090    {
091        return getGeneralizedTime( new Date( time ) );
092    }
093
094}