1 13 package com.tonbeller.wcf.format; 14 15 import java.text.ParsePosition ; 16 import java.text.SimpleDateFormat ; 17 import java.util.Date ; 18 import java.util.List ; 19 import java.util.Locale ; 20 21 22 30 public class DateHandler extends FormatHandlerSupport { 31 32 public String format(Object o, String userPattern) { 33 if (o == null) { 34 return ""; 35 } 36 37 Locale locale = getLocale(); 38 SimpleDateFormat sdf = new SimpleDateFormat (findPattern(userPattern), locale); 39 40 return sdf.format(o); 41 } 42 43 public Object parse(String s, String userPattern) throws FormatException { 44 if (s == null) { 45 return null; 46 } 47 48 s = s.trim(); 49 if (s.length() == 0) { 50 return null; 51 } 52 53 SimpleDateFormat sdf = new SimpleDateFormat (findPattern(userPattern), getLocale()); 54 ParsePosition pos = new ParsePosition (0); 55 Object o = sdf.parse(s, pos); 56 57 if ((o == null) || (pos.getIndex() != s.length())) { 58 throw new FormatException(getErrorMessage(s)); 59 } 60 61 return o; 62 } 63 64 public boolean canHandle(Object value) { 65 return value instanceof Date ; 66 } 67 68 69 public Object toNativeArray(List list) { 70 Date [] array = new Date [list.size()]; 71 for (int i = 0; i < array.length; i++) 72 array[i] = (Date )list.get(i); 73 return array; 74 } 75 76 public Object [] toObjectArray(Object value) { 77 if (value instanceof Date ) 78 return new Date []{(Date )value}; 79 return (Date [])value; 80 } 81 82 } | Popular Tags |