1 package JSci.text; 2 3 import java.text.*; 4 import java.util.Date ; 5 6 13 public final class TimeFormat extends NumberFormat { 14 private DateFormat dateFormat; 15 private double scale; 16 private long offset; 17 18 22 public TimeFormat(DateFormat dateFormat, double scale, long offset) { 23 this.dateFormat = dateFormat; 24 this.scale = scale; 25 this.offset = offset; 26 } 27 private Date toDate(double x) { 28 return new Date (((long)(scale*x))+offset); 29 } 30 32 public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) { 33 pos.setBeginIndex(-1); 34 pos.setEndIndex(-1); 35 return toAppendTo.append(dateFormat.format(toDate(number))); 36 } 37 public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) { 38 pos.setBeginIndex(-1); 39 pos.setEndIndex(-1); 40 return toAppendTo.append(dateFormat.format(toDate(number))); 41 } 42 private Number toNumber(Date date) { 43 return new Double ((date.getTime()-offset)/scale); 44 } 45 public Number parse(String text, ParsePosition parsePosition) { 46 return toNumber(dateFormat.parse(text, parsePosition)); 47 } 48 } 49 50 | Popular Tags |