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