1 16 17 package org.apache.tester; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.text.ParsePosition ; 21 import java.text.SimpleDateFormat ; 22 import java.sql.Date ; 23 24 30 public class DatePropertyEditor extends PropertyEditorSupport { 31 32 33 35 36 40 private SimpleDateFormat format = new SimpleDateFormat ("MM/dd/yyyy"); 41 42 43 45 46 51 public String getAsText() { 52 53 try { 54 Date date = (Date ) getValue(); 55 return (format.format(date)); 56 } catch (ClassCastException e) { 57 return (null); 58 } catch (IllegalArgumentException e) { 59 return (null); 60 } 61 62 } 63 64 65 73 public void setAsText(String value) throws IllegalArgumentException { 74 75 if (value == null) 77 throw new IllegalArgumentException 78 ("Cannot convert null String to a Date"); 79 if (value.length() != 10) 80 throw new IllegalArgumentException 81 ("String '" + value + "' has invalid length " + 82 value.length()); 83 for (int i = 0; i < 10; i++) { 84 char ch = value.charAt(i); 85 if ((i == 2) || (i == 5)) { 86 if (ch != '/') 87 throw new IllegalArgumentException 88 ("String '" + value + "' missing slash at index " + 89 i); 90 } else { 91 if (!Character.isDigit(ch)) 92 throw new IllegalArgumentException 93 ("String '" + value + "' missing digit at index " + 94 i); 95 } 96 } 97 98 java.util.Date temp = format.parse(value, new ParsePosition (0)); 100 java.sql.Date date = new java.sql.Date (temp.getTime()); 101 setValue(date); 102 } 103 104 105 } 106 | Popular Tags |