1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.kernel.util.DateFormatFactory;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  
28  import java.text.DateFormat;
29  import java.text.SimpleDateFormat;
30  
31  import java.util.Locale;
32  import java.util.TimeZone;
33  
34  /**
35   * <a href="DateFormatFactoryImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class DateFormatFactoryImpl implements DateFormatFactory {
40  
41      public DateFormat getDate(Locale locale) {
42          return getDate(locale, null);
43      }
44  
45      public DateFormat getDate(Locale locale, TimeZone timeZone) {
46          DateFormat dateFormat = DateFormat.getDateInstance(
47              DateFormat.SHORT, locale);
48  
49          if (timeZone != null) {
50              dateFormat.setTimeZone(timeZone);
51          }
52  
53          return dateFormat;
54      }
55  
56      public DateFormat getDate(TimeZone timeZone) {
57          return getDate(LocaleUtil.getDefault(), timeZone);
58      }
59  
60      public DateFormat getDateTime(Locale locale) {
61          return getDateTime(locale, null);
62      }
63  
64      public DateFormat getDateTime(Locale locale, TimeZone timeZone) {
65          DateFormat dateFormat = DateFormat.getDateTimeInstance(
66              DateFormat.SHORT, DateFormat.SHORT, locale);
67  
68          if (timeZone != null) {
69              dateFormat.setTimeZone(timeZone);
70          }
71  
72          return dateFormat;
73      }
74  
75      public DateFormat getDateTime(TimeZone timeZone) {
76          return getDateTime(LocaleUtil.getDefault(), timeZone);
77      }
78  
79      public DateFormat getSimpleDateFormat(String pattern) {
80          return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
81      }
82  
83      public DateFormat getSimpleDateFormat(String pattern, Locale locale) {
84          return getSimpleDateFormat(pattern, locale, null);
85      }
86  
87      public DateFormat getSimpleDateFormat(
88          String pattern, Locale locale, TimeZone timeZone) {
89  
90          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
91  
92          if (timeZone != null) {
93              dateFormat.setTimeZone(timeZone);
94          }
95  
96          return dateFormat;
97      }
98  
99      public DateFormat getSimpleDateFormat(String pattern, TimeZone timeZone) {
100         return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
101     }
102 
103     public DateFormat getTime(Locale locale) {
104         return getTime(locale, null);
105     }
106 
107     public DateFormat getTime(Locale locale, TimeZone timeZone) {
108         DateFormat dateFormat = DateFormat.getTimeInstance(
109             DateFormat.SHORT, locale);
110 
111         if (timeZone != null) {
112             dateFormat.setTimeZone(timeZone);
113         }
114 
115         return dateFormat;
116     }
117 
118     public DateFormat getTime(TimeZone timeZone) {
119         return getTime(LocaleUtil.getDefault(), timeZone);
120     }
121 
122 }