1
14
15 package com.liferay.portal.util;
16
17 import com.liferay.portal.kernel.util.FastDateFormatFactory;
18 import com.liferay.portal.kernel.util.LocaleUtil;
19 import com.liferay.portal.kernel.util.StringBundler;
20 import com.liferay.portal.kernel.util.StringPool;
21
22 import java.text.Format;
23
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.TimeZone;
27 import java.util.concurrent.ConcurrentHashMap;
28
29 import org.apache.commons.lang.time.FastDateFormat;
30
31
36 public class FastDateFormatFactoryImpl implements FastDateFormatFactory {
37
38 public Format getDate(Locale locale) {
39 return getDate(locale, null);
40 }
41
42 public Format getDate(Locale locale, TimeZone timeZone) {
43 String key = getKey(locale, timeZone);
44
45 Format format = _dateFormats.get(key);
46
47 if (format == null) {
48 format = FastDateFormat.getDateInstance(
49 FastDateFormat.SHORT, timeZone, locale);
50
51 _dateFormats.put(key, format);
52 }
53
54 return format;
55 }
56
57 public Format getDate(TimeZone timeZone) {
58 return getDate(LocaleUtil.getDefault(), timeZone);
59 }
60
61 public Format getDateTime(Locale locale) {
62 return getDateTime(locale, null);
63 }
64
65 public Format getDateTime(Locale locale, TimeZone timeZone) {
66 String key = getKey(locale, timeZone);
67
68 Format format = _dateTimeFormats.get(key);
69
70 if (format == null) {
71 format = FastDateFormat.getDateTimeInstance(
72 FastDateFormat.SHORT, FastDateFormat.SHORT, timeZone, locale);
73
74 _dateTimeFormats.put(key, format);
75 }
76
77 return format;
78 }
79
80 public Format getDateTime(TimeZone timeZone) {
81 return getDateTime(LocaleUtil.getDefault(), timeZone);
82 }
83
84 public Format getSimpleDateFormat(String pattern) {
85 return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
86 }
87
88 public Format getSimpleDateFormat(String pattern, Locale locale) {
89 return getSimpleDateFormat(pattern, locale, null);
90 }
91
92 public Format getSimpleDateFormat(
93 String pattern, Locale locale, TimeZone timeZone) {
94
95 String key = getKey(pattern, locale, timeZone);
96
97 Format format = _simpleDateFormats.get(key);
98
99 if (format == null) {
100 format = FastDateFormat.getInstance(pattern, timeZone, locale);
101
102 _simpleDateFormats.put(key, format);
103 }
104
105 return format;
106 }
107
108 public Format getSimpleDateFormat(String pattern, TimeZone timeZone) {
109 return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
110 }
111
112 public Format getTime(Locale locale) {
113 return getTime(locale, null);
114 }
115
116 public Format getTime(Locale locale, TimeZone timeZone) {
117 String key = getKey(locale, timeZone);
118
119 Format format = _timeFormats.get(key);
120
121 if (format == null) {
122 format = FastDateFormat.getTimeInstance(
123 FastDateFormat.SHORT, timeZone, locale);
124
125 _timeFormats.put(key, format);
126 }
127
128 return format;
129 }
130
131 public Format getTime(TimeZone timeZone) {
132 return getTime(LocaleUtil.getDefault(), timeZone);
133 }
134
135 protected String getKey(Locale locale, TimeZone timeZone) {
136 return String.valueOf(locale).concat(StringPool.UNDERLINE).concat(
137 String.valueOf(timeZone));
138 }
139
140 protected String getKey(String pattern, Locale locale, TimeZone timeZone) {
141 StringBundler sb = new StringBundler(5);
142
143 sb.append(pattern);
144 sb.append(StringPool.UNDERLINE);
145 sb.append(String.valueOf(locale));
146 sb.append(StringPool.UNDERLINE);
147 sb.append(String.valueOf(timeZone));
148
149 return sb.toString();
150 }
151
152 private Map<String, Format> _dateFormats =
153 new ConcurrentHashMap<String, Format>();
154 private Map<String, Format> _dateTimeFormats =
155 new ConcurrentHashMap<String, Format>();
156 private Map<String, Format> _simpleDateFormats =
157 new ConcurrentHashMap<String, Format>();
158 private Map<String, Format> _timeFormats =
159 new ConcurrentHashMap<String, Format>();
160
161 }