001package com.hfg.datetime; 002 003import java.text.*; 004import java.util.Calendar; 005import java.util.Date; 006import java.util.Locale; 007import java.util.TimeZone; 008 009//------------------------------------------------------------------------------ 010/** 011 * Thread-safe extension of SimpleDateFormat 012 * 013 * @author J. Alex Taylor, hairyfatguy.com 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class ThreadSafeDateFormat extends SimpleDateFormat 037{ 038 ThreadLocal<SimpleDateFormat> mDateFormat; 039 040 //########################################################################### 041 // CONSTRUCTORS 042 //########################################################################### 043 044 //-------------------------------------------------------------------------- 045 public ThreadSafeDateFormat() 046 { 047 super(); 048 mDateFormat = new ThreadLocal<SimpleDateFormat>() 049 { 050 protected SimpleDateFormat initialValue() 051 { 052 return new SimpleDateFormat(); 053 } 054 }; 055 } 056 057 //-------------------------------------------------------------------------- 058 public ThreadSafeDateFormat(final String inPattern) 059 { 060 super(); 061 mDateFormat = new ThreadLocal<SimpleDateFormat>() 062 { 063 protected SimpleDateFormat initialValue() 064 { 065 return new SimpleDateFormat(inPattern); 066 } 067 }; 068 } 069 070 //-------------------------------------------------------------------------- 071 public ThreadSafeDateFormat(final String inPattern, final DateFormatSymbols formatSymbols) 072 { 073 super(); 074 mDateFormat = new ThreadLocal<SimpleDateFormat>() 075 { 076 protected SimpleDateFormat initialValue() 077 { 078 return new SimpleDateFormat(inPattern, formatSymbols); 079 } 080 }; 081 } 082 083 //-------------------------------------------------------------------------- 084 public ThreadSafeDateFormat(final String inPattern, final Locale locale) 085 { 086 super(); 087 mDateFormat = new ThreadLocal<SimpleDateFormat>() 088 { 089 protected SimpleDateFormat initialValue() 090 { 091 return new SimpleDateFormat(inPattern, locale); 092 } 093 }; 094 } 095 096 //########################################################################### 097 // PUBLIC METHODS 098 //########################################################################### 099 100 //-------------------------------------------------------------------------- 101 @Override 102 public Object parseObject(String source) 103 throws ParseException 104 { 105 return mDateFormat.get().parseObject(source); 106 } 107 108 //-------------------------------------------------------------------------- 109 @Override 110 public String toString() 111 { 112 return mDateFormat.get().toString(); 113 } 114 115 //-------------------------------------------------------------------------- 116 @Override 117 public Date parse(String source) 118 throws ParseException 119 { 120 return mDateFormat.get().parse(source); 121 } 122 123 //-------------------------------------------------------------------------- 124 @Override 125 public Object clone() 126 { 127 return mDateFormat.get().clone(); 128 } 129 130 //-------------------------------------------------------------------------- 131 @Override 132 public int hashCode() 133 { 134 return mDateFormat.get().hashCode(); 135 } 136 137 //-------------------------------------------------------------------------- 138 @Override 139 public boolean equals(Object obj) 140 { 141 return mDateFormat.get().equals(obj); 142 } 143 144 //-------------------------------------------------------------------------- 145 @Override 146 public Object parseObject(String source, ParsePosition pos) 147 { 148 return mDateFormat.get().parseObject(source, pos); 149 } 150 151 //-------------------------------------------------------------------------- 152 @Override 153 public void setCalendar(Calendar newCalendar) 154 { 155 mDateFormat.get().setCalendar(newCalendar); 156 } 157 158 //-------------------------------------------------------------------------- 159 @Override 160 public Calendar getCalendar() 161 { 162 return mDateFormat.get().getCalendar(); 163 } 164 165 //-------------------------------------------------------------------------- 166 @Override 167 public void setNumberFormat(NumberFormat newNumberFormat) 168 { 169 mDateFormat.get().setNumberFormat(newNumberFormat); 170 } 171 172 //-------------------------------------------------------------------------- 173 @Override 174 public NumberFormat getNumberFormat() 175 { 176 return mDateFormat.get().getNumberFormat(); 177 } 178 179 //-------------------------------------------------------------------------- 180 @Override 181 public void setTimeZone(TimeZone zone) 182 { 183 mDateFormat.get().setTimeZone(zone); 184 } 185 186 //-------------------------------------------------------------------------- 187 @Override 188 public TimeZone getTimeZone() 189 { 190 return mDateFormat.get().getTimeZone(); 191 } 192 193 //-------------------------------------------------------------------------- 194 @Override 195 public void setLenient(boolean lenient) 196 { 197 mDateFormat.get().setLenient(lenient); 198 } 199 200 //-------------------------------------------------------------------------- 201 @Override 202 public boolean isLenient() 203 { 204 return mDateFormat.get().isLenient(); 205 } 206 207 //-------------------------------------------------------------------------- 208 @Override 209 public void set2DigitYearStart(Date startDate) 210 { 211 mDateFormat.get().set2DigitYearStart(startDate); 212 } 213 214 //-------------------------------------------------------------------------- 215 @Override 216 public Date get2DigitYearStart() 217 { 218 return mDateFormat.get().get2DigitYearStart(); 219 } 220 221 //-------------------------------------------------------------------------- 222 @Override 223 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) 224 { 225 return mDateFormat.get().format(date, toAppendTo, pos); 226 } 227 228 //-------------------------------------------------------------------------- 229 @Override 230 public AttributedCharacterIterator formatToCharacterIterator(Object obj) 231 { 232 return mDateFormat.get().formatToCharacterIterator(obj); 233 } 234 235 //-------------------------------------------------------------------------- 236 @Override 237 public Date parse(String text, ParsePosition pos) 238 { 239 return mDateFormat.get().parse(text, pos); 240 } 241 242 //-------------------------------------------------------------------------- 243 @Override 244 public String toPattern() 245 { 246 return mDateFormat.get().toPattern(); 247 } 248 249 //-------------------------------------------------------------------------- 250 @Override 251 public String toLocalizedPattern() 252 { 253 return mDateFormat.get().toLocalizedPattern(); 254 } 255 256 //-------------------------------------------------------------------------- 257 @Override 258 public void applyPattern(String pattern) 259 { 260 mDateFormat.get().applyPattern(pattern); 261 } 262 263 //-------------------------------------------------------------------------- 264 @Override 265 public void applyLocalizedPattern(String pattern) 266 { 267 mDateFormat.get().applyLocalizedPattern(pattern); 268 } 269 270 //-------------------------------------------------------------------------- 271 @Override 272 public DateFormatSymbols getDateFormatSymbols() 273 { 274 return mDateFormat.get().getDateFormatSymbols(); 275 } 276 277 //-------------------------------------------------------------------------- 278 @Override 279 public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols) 280 { 281 mDateFormat.get().setDateFormatSymbols(newFormatSymbols); 282 } 283 284}