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