001    /**
002     * Copyright (c) 2000-2013 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    
021    import java.text.DateFormat;
022    
023    import java.util.Locale;
024    import java.util.TimeZone;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    @DoPrivileged
030    public class DateFormatFactoryImpl implements DateFormatFactory {
031    
032            @Override
033            public DateFormat getDate(Locale locale) {
034                    return getDate(locale, null);
035            }
036    
037            @Override
038            public DateFormat getDate(Locale locale, TimeZone timeZone) {
039                    DateFormat dateFormat = DateFormat.getDateInstance(
040                            DateFormat.SHORT, locale);
041    
042                    if (timeZone != null) {
043                            dateFormat.setTimeZone(timeZone);
044                    }
045    
046                    return dateFormat;
047            }
048    
049            @Override
050            public DateFormat getDate(TimeZone timeZone) {
051                    return getDate(LocaleUtil.getDefault(), timeZone);
052            }
053    
054            @Override
055            public DateFormat getDateTime(Locale locale) {
056                    return getDateTime(locale, null);
057            }
058    
059            @Override
060            public DateFormat getDateTime(Locale locale, TimeZone timeZone) {
061                    DateFormat dateFormat = DateFormat.getDateTimeInstance(
062                            DateFormat.SHORT, DateFormat.SHORT, locale);
063    
064                    if (timeZone != null) {
065                            dateFormat.setTimeZone(timeZone);
066                    }
067    
068                    return dateFormat;
069            }
070    
071            @Override
072            public DateFormat getDateTime(TimeZone timeZone) {
073                    return getDateTime(LocaleUtil.getDefault(), timeZone);
074            }
075    
076            @Override
077            public DateFormat getSimpleDateFormat(String pattern) {
078                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
079            }
080    
081            @Override
082            public DateFormat getSimpleDateFormat(String pattern, Locale locale) {
083                    return getSimpleDateFormat(pattern, locale, null);
084            }
085    
086            @Override
087            public DateFormat getSimpleDateFormat(
088                    String pattern, Locale locale, TimeZone timeZone) {
089    
090                    DateFormat dateFormat = new PortalSimpleDateFormat(pattern, locale);
091    
092                    if (timeZone != null) {
093                            dateFormat.setTimeZone(timeZone);
094                    }
095    
096                    return dateFormat;
097            }
098    
099            @Override
100            public DateFormat getSimpleDateFormat(String pattern, TimeZone timeZone) {
101                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
102            }
103    
104            @Override
105            public DateFormat getTime(Locale locale) {
106                    return getTime(locale, null);
107            }
108    
109            @Override
110            public DateFormat getTime(Locale locale, TimeZone timeZone) {
111                    DateFormat dateFormat = DateFormat.getTimeInstance(
112                            DateFormat.SHORT, locale);
113    
114                    if (timeZone != null) {
115                            dateFormat.setTimeZone(timeZone);
116                    }
117    
118                    return dateFormat;
119            }
120    
121            @Override
122            public DateFormat getTime(TimeZone timeZone) {
123                    return getTime(LocaleUtil.getDefault(), timeZone);
124            }
125    
126    }