1 5 6 package org.exoplatform.services.workflow.format; 7 8 import java.text.*; 9 import java.util.Date ; 10 import java.util.Locale ; 11 12 17 public class BasicDateFormat extends Format{ 18 private SimpleDateFormat dateFormat; 19 private String pattern = "dd/MM/yyyy"; 20 private Locale locale = new Locale ("en"); 21 22 public BasicDateFormat() { 23 updateFormat(); 24 } 25 26 public void setPattern(String pattern){ 27 this.pattern = pattern; 28 updateFormat(); 29 } 30 31 public void setLocale(Locale locale){ 32 this.locale = locale; 33 updateFormat(); 34 } 35 36 public void updateFormat(){ 37 dateFormat = new SimpleDateFormat(pattern, locale); 38 } 39 40 public Object parseObject(String source, ParsePosition pos) { 41 Date result = null; 42 if ( source != null ) { 43 try { 44 result = dateFormat.parse(source); 45 pos.setErrorIndex(-1); 46 pos.setIndex(source.length()); 47 } 48 catch(ParseException e) { 49 pos.setErrorIndex(e.getErrorOffset()); 50 e.printStackTrace(); 51 } 52 } 53 return result; 54 } 55 56 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 57 if ( obj != null ) { 58 toAppendTo.append(dateFormat.format(obj)); 59 } 60 return toAppendTo; 61 } 62 63 } 64 | Popular Tags |