1 16 17 package org.apache.commons.beanutils.locale.converters; 18 19 import org.apache.commons.beanutils.locale.BaseLocaleConverter; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.commons.logging.Log; 22 23 import java.text.ParseException ; 24 import java.text.ParsePosition ; 25 import java.text.SimpleDateFormat ; 26 import java.util.Locale ; 27 28 29 40 41 public class DateLocaleConverter extends BaseLocaleConverter { 42 43 45 46 private static Log log = LogFactory.getLog(DateLocaleConverter.class); 47 48 49 boolean isLenient = false; 50 51 53 61 public DateLocaleConverter() { 62 63 this(false); 64 } 65 66 74 public DateLocaleConverter(boolean locPattern) { 75 76 this(Locale.getDefault(), locPattern); 77 } 78 79 86 public DateLocaleConverter(Locale locale) { 87 88 this(locale, false); 89 } 90 91 99 public DateLocaleConverter(Locale locale, boolean locPattern) { 100 101 this(locale, (String ) null, locPattern); 102 } 103 104 112 public DateLocaleConverter(Locale locale, String pattern) { 113 114 this(locale, pattern, false); 115 } 116 117 126 public DateLocaleConverter(Locale locale, String pattern, boolean locPattern) { 127 128 super(locale, pattern, locPattern); 129 } 130 131 140 public DateLocaleConverter(Object defaultValue) { 141 142 this(defaultValue, false); 143 } 144 145 154 public DateLocaleConverter(Object defaultValue, boolean locPattern) { 155 156 this(defaultValue, Locale.getDefault(), locPattern); 157 } 158 159 167 public DateLocaleConverter(Object defaultValue, Locale locale) { 168 169 this(defaultValue, locale, false); 170 } 171 172 181 public DateLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) { 182 183 this(defaultValue, locale, null, locPattern); 184 } 185 186 187 196 public DateLocaleConverter(Object defaultValue, Locale locale, String pattern) { 197 198 this(defaultValue, locale, pattern, false); 199 } 200 201 211 public DateLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { 212 213 super(defaultValue, locale, pattern, locPattern); 214 } 215 216 218 224 public boolean isLenient() { 225 return isLenient; 226 } 227 228 234 public void setLenient(boolean lenient) { 235 isLenient = lenient; 236 } 237 238 240 250 protected Object parse(Object value, String pattern) throws ParseException { 251 SimpleDateFormat formatter = getFormatter(pattern, locale); 252 if (locPattern) { 253 formatter.applyLocalizedPattern(pattern); 254 } 255 else { 256 formatter.applyPattern(pattern); 257 } 258 return formatter.parse((String ) value); 259 } 260 261 265 private SimpleDateFormat getFormatter(String pattern, Locale locale) { 266 if(pattern == null) { 271 pattern = locPattern ? 272 new SimpleDateFormat ().toLocalizedPattern() : new SimpleDateFormat ().toPattern(); 273 log.warn("Null pattern was provided, defaulting to: " + pattern); 274 } 275 SimpleDateFormat format = new SimpleDateFormat (pattern, locale); 276 format.setLenient(isLenient); 277 return format; 278 } 279 } 280 | Popular Tags |