1 21 22 package org.apache.commons.validator; 23 24 import java.text.DateFormat ; 25 import java.text.ParseException ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Locale ; 28 29 38 public class DateValidator { 39 40 43 private static final DateValidator instance = new DateValidator(); 44 45 48 public static DateValidator getInstance() { 49 return instance; 50 } 51 52 55 protected DateValidator() { 56 super(); 57 } 58 59 70 public boolean isValid(String value, String datePattern, boolean strict) { 71 72 if (value == null 73 || datePattern == null 74 || datePattern.length() <= 0) { 75 76 return false; 77 } 78 79 SimpleDateFormat formatter = new SimpleDateFormat (datePattern); 80 formatter.setLenient(false); 81 82 try { 83 formatter.parse(value); 84 } catch(ParseException e) { 85 return false; 86 } 87 88 if (strict && (datePattern.length() != value.length())) { 89 return false; 90 } 91 92 return true; 93 } 94 95 104 public boolean isValid(String value, Locale locale) { 105 106 if (value == null) { 107 return false; 108 } 109 110 DateFormat formatter = null; 111 if (locale != null) { 112 formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale); 113 } else { 114 formatter = 115 DateFormat.getDateInstance( 116 DateFormat.SHORT, 117 Locale.getDefault()); 118 } 119 120 formatter.setLenient(false); 121 122 try { 123 formatter.parse(value); 124 } catch(ParseException e) { 125 return false; 126 } 127 128 return true; 129 } 130 131 } 132 | Popular Tags |