1 4 package org.oddjob.values.types; 5 6 import java.text.DateFormat ; 7 import java.text.DecimalFormat ; 8 import java.text.NumberFormat ; 9 import java.text.ParseException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.Date ; 12 import java.util.TimeZone ; 13 14 29 public class FormatType { 30 31 36 private String format; 37 38 43 private TimeZone timeZone; 44 45 50 private Date date; 51 52 57 private Number number; 58 59 public String valueFor(Class required) throws ParseException { 60 if (format == null) { 61 return null; 62 } 63 if (date != null) { 64 DateFormat dateFormat = new SimpleDateFormat (format); 65 if (timeZone != null) { 66 dateFormat.setTimeZone(timeZone); 67 } 68 return dateFormat.format(date); 69 } 70 if (number != null) { 71 NumberFormat numberFormat = new DecimalFormat (format); 72 return numberFormat.format(number); 73 } 74 return null; 75 } 76 77 public void setDate(Date date) { 78 if (date == null) { 79 this.date = null; 80 } 81 else { 82 this.date = new Date (date.getTime()); 83 } 84 } 85 86 public void setFormat(String format) { 87 this.format = format; 88 } 89 90 public void setTimeZone(String timeZoneId) { 91 this.timeZone = TimeZone.getTimeZone(timeZoneId); 92 } 93 94 public void setNumber(Number number) { 95 this.number = number; 96 } 97 98 public String toString() { 99 try { 100 return valueFor(null); 101 } catch (ParseException e) { 102 return e.getMessage(); 103 } 104 } 105 } 106 | Popular Tags |