001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.util;
029
030import java.util.Date;
031
032import com.google.gwt.i18n.client.DateTimeFormat;
033
034/**
035 * Client side implementation for {@link org.opencms.util.CmsDateUtil}.<p>
036 *
037 * @since 8.0.0
038 *
039 * @see org.opencms.util.CmsDateUtil
040 */
041public final class CmsDateTimeUtil {
042
043    /**
044     * The standard formats.<p>
045     *
046     * @see java.text.DateFormat
047     */
048    public enum Format {
049
050        /** @see java.text.DateFormat#FULL */
051        FULL,
052
053        /** @see java.text.DateFormat#LONG */
054        LONG,
055
056        /** @see java.text.DateFormat#MEDIUM */
057        MEDIUM,
058
059        /** @see java.text.DateFormat#SHORT */
060        SHORT;
061    }
062
063    /**
064     * Hides the public constructor.<p>
065     */
066    private CmsDateTimeUtil() {
067
068        // noop
069    }
070
071    /**
072     * Returns a formated date String from a Date value,
073     * the formatting based on the provided options.<p>
074     *
075     * @param date the Date object to format as String
076     * @param format the format to use
077     *
078     * @return the formatted date
079     */
080    public static String getDate(Date date, Format format) {
081
082        DateTimeFormat df;
083        switch (format) {
084            case FULL:
085                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_FULL);
086                break;
087            case LONG:
088                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_LONG);
089                break;
090            case MEDIUM:
091                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_MEDIUM);
092                break;
093            case SHORT:
094                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT);
095                break;
096            default:
097                // can never happen, just to prevent stupid warning
098                return "";
099        }
100        return df.format(date);
101    }
102
103    /**
104     * Returns a formated date String form a timestamp value,
105     * the formatting based on the OpenCms system default locale
106     * and the {@link Format#SHORT} date format.<p>
107     *
108     * @param time the time value to format as date
109     * @return the formatted date
110     */
111    public static String getDateShort(long time) {
112
113        return getDate(new Date(time), Format.SHORT);
114    }
115
116    /**
117     * Returns a formated date and time String from a Date value,
118     * the formatting based on the provided options.<p>
119     *
120     * @param date the Date object to format as String
121     * @param format the format to use, see {@link Format} for possible values
122     * @return the formatted date
123     */
124    public static String getDateTime(Date date, Format format) {
125
126        StringBuffer buf = new StringBuffer();
127        buf.append(getDate(date, format));
128        buf.append(" ");
129        buf.append(getTime(date, format));
130        return buf.toString();
131    }
132
133    /**
134     * Returns a formated date and time String form a timestamp value,
135     * the formatting based on the OpenCms system default locale
136     * and the {@link Format#SHORT} date format.<p>
137     *
138     * @param time the time value to format as date
139     * @return the formatted date
140     */
141    public static String getDateTimeShort(long time) {
142
143        return getDateTime(new Date(time), Format.SHORT);
144    }
145
146    /**
147     * Returns a formated time String from a Date value,
148     * the formatting based on the provided options.<p>
149     *
150     * @param date the Date object to format as String
151     * @param format the format to use
152     *
153     * @return the formatted time
154     */
155    public static String getTime(Date date, Format format) {
156
157        DateTimeFormat df;
158        switch (format) {
159            case FULL:
160                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_FULL);
161                break;
162            case LONG:
163                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_LONG);
164                break;
165            case MEDIUM:
166                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_MEDIUM);
167                break;
168            case SHORT:
169                df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_SHORT);
170                break;
171            default:
172                // can never happen, just to prevent stupid warning
173                return "";
174        }
175        return df.format(date);
176    }
177}