1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.util.DateFormatFactory;
18  import com.liferay.portal.kernel.util.LocaleUtil;
19  
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  
23  import java.util.Locale;
24  import java.util.TimeZone;
25  
26  /**
27   * <a href="DateFormatFactoryImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class DateFormatFactoryImpl implements DateFormatFactory {
32  
33      public DateFormat getDate(Locale locale) {
34          return getDate(locale, null);
35      }
36  
37      public DateFormat getDate(Locale locale, TimeZone timeZone) {
38          DateFormat dateFormat = DateFormat.getDateInstance(
39              DateFormat.SHORT, locale);
40  
41          if (timeZone != null) {
42              dateFormat.setTimeZone(timeZone);
43          }
44  
45          return dateFormat;
46      }
47  
48      public DateFormat getDate(TimeZone timeZone) {
49          return getDate(LocaleUtil.getDefault(), timeZone);
50      }
51  
52      public DateFormat getDateTime(Locale locale) {
53          return getDateTime(locale, null);
54      }
55  
56      public DateFormat getDateTime(Locale locale, TimeZone timeZone) {
57          DateFormat dateFormat = DateFormat.getDateTimeInstance(
58              DateFormat.SHORT, DateFormat.SHORT, locale);
59  
60          if (timeZone != null) {
61              dateFormat.setTimeZone(timeZone);
62          }
63  
64          return dateFormat;
65      }
66  
67      public DateFormat getDateTime(TimeZone timeZone) {
68          return getDateTime(LocaleUtil.getDefault(), timeZone);
69      }
70  
71      public DateFormat getSimpleDateFormat(String pattern) {
72          return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
73      }
74  
75      public DateFormat getSimpleDateFormat(String pattern, Locale locale) {
76          return getSimpleDateFormat(pattern, locale, null);
77      }
78  
79      public DateFormat getSimpleDateFormat(
80          String pattern, Locale locale, TimeZone timeZone) {
81  
82          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
83  
84          if (timeZone != null) {
85              dateFormat.setTimeZone(timeZone);
86          }
87  
88          return dateFormat;
89      }
90  
91      public DateFormat getSimpleDateFormat(String pattern, TimeZone timeZone) {
92          return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
93      }
94  
95      public DateFormat getTime(Locale locale) {
96          return getTime(locale, null);
97      }
98  
99      public DateFormat getTime(Locale locale, TimeZone timeZone) {
100         DateFormat dateFormat = DateFormat.getTimeInstance(
101             DateFormat.SHORT, locale);
102 
103         if (timeZone != null) {
104             dateFormat.setTimeZone(timeZone);
105         }
106 
107         return dateFormat;
108     }
109 
110     public DateFormat getTime(TimeZone timeZone) {
111         return getTime(LocaleUtil.getDefault(), timeZone);
112     }
113 
114 }