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            public DateFormat getDate(Locale locale) {
033                    return getDate(locale, null);
034            }
035    
036            public DateFormat getDate(Locale locale, TimeZone timeZone) {
037                    DateFormat dateFormat = DateFormat.getDateInstance(
038                            DateFormat.SHORT, locale);
039    
040                    if (timeZone != null) {
041                            dateFormat.setTimeZone(timeZone);
042                    }
043    
044                    return dateFormat;
045            }
046    
047            public DateFormat getDate(TimeZone timeZone) {
048                    return getDate(LocaleUtil.getDefault(), timeZone);
049            }
050    
051            public DateFormat getDateTime(Locale locale) {
052                    return getDateTime(locale, null);
053            }
054    
055            public DateFormat getDateTime(Locale locale, TimeZone timeZone) {
056                    DateFormat dateFormat = DateFormat.getDateTimeInstance(
057                            DateFormat.SHORT, DateFormat.SHORT, locale);
058    
059                    if (timeZone != null) {
060                            dateFormat.setTimeZone(timeZone);
061                    }
062    
063                    return dateFormat;
064            }
065    
066            public DateFormat getDateTime(TimeZone timeZone) {
067                    return getDateTime(LocaleUtil.getDefault(), timeZone);
068            }
069    
070            public DateFormat getSimpleDateFormat(String pattern) {
071                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
072            }
073    
074            public DateFormat getSimpleDateFormat(String pattern, Locale locale) {
075                    return getSimpleDateFormat(pattern, locale, null);
076            }
077    
078            public DateFormat getSimpleDateFormat(
079                    String pattern, Locale locale, TimeZone timeZone) {
080    
081                    DateFormat dateFormat = new PortalSimpleDateFormat(pattern, locale);
082    
083                    if (timeZone != null) {
084                            dateFormat.setTimeZone(timeZone);
085                    }
086    
087                    return dateFormat;
088            }
089    
090            public DateFormat getSimpleDateFormat(String pattern, TimeZone timeZone) {
091                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
092            }
093    
094            public DateFormat getTime(Locale locale) {
095                    return getTime(locale, null);
096            }
097    
098            public DateFormat getTime(Locale locale, TimeZone timeZone) {
099                    DateFormat dateFormat = DateFormat.getTimeInstance(
100                            DateFormat.SHORT, locale);
101    
102                    if (timeZone != null) {
103                            dateFormat.setTimeZone(timeZone);
104                    }
105    
106                    return dateFormat;
107            }
108    
109            public DateFormat getTime(TimeZone timeZone) {
110                    return getTime(LocaleUtil.getDefault(), timeZone);
111            }
112    
113    }