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