001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.util.DateFormatFactory;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.PortalSimpleDateFormat;
021    
022    import java.text.DateFormat;
023    
024    import java.util.Locale;
025    import java.util.TimeZone;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    @DoPrivileged
031    public class DateFormatFactoryImpl implements DateFormatFactory {
032    
033            @Override
034            public DateFormat getDate(Locale locale) {
035                    return getDate(locale, null);
036            }
037    
038            @Override
039            public DateFormat getDate(Locale locale, TimeZone timeZone) {
040                    DateFormat dateFormat = DateFormat.getDateInstance(
041                            DateFormat.SHORT, locale);
042    
043                    if (timeZone != null) {
044                            dateFormat.setTimeZone(timeZone);
045                    }
046    
047                    return dateFormat;
048            }
049    
050            @Override
051            public DateFormat getDate(TimeZone timeZone) {
052                    return getDate(LocaleUtil.getDefault(), timeZone);
053            }
054    
055            @Override
056            public DateFormat getDateTime(Locale locale) {
057                    return getDateTime(locale, null);
058            }
059    
060            @Override
061            public DateFormat getDateTime(Locale locale, TimeZone timeZone) {
062                    DateFormat dateFormat = DateFormat.getDateTimeInstance(
063                            DateFormat.SHORT, DateFormat.SHORT, locale);
064    
065                    if (timeZone != null) {
066                            dateFormat.setTimeZone(timeZone);
067                    }
068    
069                    return dateFormat;
070            }
071    
072            @Override
073            public DateFormat getDateTime(TimeZone timeZone) {
074                    return getDateTime(LocaleUtil.getDefault(), timeZone);
075            }
076    
077            @Override
078            public DateFormat getSimpleDateFormat(String pattern) {
079                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
080            }
081    
082            @Override
083            public DateFormat getSimpleDateFormat(String pattern, Locale locale) {
084                    return getSimpleDateFormat(pattern, locale, null);
085            }
086    
087            @Override
088            public DateFormat getSimpleDateFormat(
089                    String pattern, Locale locale, TimeZone timeZone) {
090    
091                    DateFormat dateFormat = new PortalSimpleDateFormat(pattern, locale);
092    
093                    if (timeZone != null) {
094                            dateFormat.setTimeZone(timeZone);
095                    }
096    
097                    return dateFormat;
098            }
099    
100            @Override
101            public DateFormat getSimpleDateFormat(String pattern, TimeZone timeZone) {
102                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
103            }
104    
105            @Override
106            public DateFormat getTime(Locale locale) {
107                    return getTime(locale, null);
108            }
109    
110            @Override
111            public DateFormat getTime(Locale locale, TimeZone timeZone) {
112                    DateFormat dateFormat = DateFormat.getTimeInstance(
113                            DateFormat.SHORT, locale);
114    
115                    if (timeZone != null) {
116                            dateFormat.setTimeZone(timeZone);
117                    }
118    
119                    return dateFormat;
120            }
121    
122            @Override
123            public DateFormat getTime(TimeZone timeZone) {
124                    return getTime(LocaleUtil.getDefault(), timeZone);
125            }
126    
127    }