1 17 18 package org.apache.jasper.util; 19 20 import java.util.Date ; 21 22 import java.text.DateFormat ; 23 import java.text.FieldPosition ; 24 import java.text.ParsePosition ; 25 import java.text.SimpleDateFormat ; 26 27 37 public class FastDateFormat extends DateFormat { 38 39 private DateFormat df; 40 private long lastSec = -1; 41 private StringBuffer sb = new StringBuffer (); 42 private FieldPosition fp = new FieldPosition (DateFormat.MILLISECOND_FIELD); 43 44 public FastDateFormat(DateFormat df) { 45 this.df = df; 46 } 47 48 public Date parse(String text, ParsePosition pos) { 49 return df.parse(text, pos); 50 } 51 52 57 public StringBuffer format(Date date, StringBuffer toAppendTo, 58 FieldPosition fieldPosition) { 59 long dt = date.getTime(); 60 long ds = dt / 1000; 61 if (ds != lastSec) { 62 sb.setLength(0); 63 df.format(date, sb, fp); 64 lastSec = ds; 65 } else { 66 int ms = (int)(dt % 1000); 68 int pos = fp.getEndIndex(); 69 int begin = fp.getBeginIndex(); 70 if (pos > 0) { 71 if (pos > begin) 72 sb.setCharAt(--pos, Character.forDigit(ms % 10, 10)); 73 ms /= 10; 74 if (pos > begin) 75 sb.setCharAt(--pos, Character.forDigit(ms % 10, 10)); 76 ms /= 10; 77 if (pos > begin) 78 sb.setCharAt(--pos, Character.forDigit(ms % 10, 10)); 79 } 80 } 81 toAppendTo.append(sb.toString()); 82 return toAppendTo; 83 } 84 85 public static void main(String [] args) { 86 String format = "yyyy-MM-dd HH:mm:ss.SSS"; 87 if (args.length > 0) 88 format = args[0]; 89 SimpleDateFormat sdf = new SimpleDateFormat (format); 90 FastDateFormat fdf = new FastDateFormat(sdf); 91 Date d = new Date (); 92 93 d.setTime(1); 94 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 95 d.setTime(20); 96 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 97 d.setTime(500); 98 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 99 d.setTime(543); 100 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 101 d.setTime(999); 102 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 103 d.setTime(1050); 104 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 105 d.setTime(2543); 106 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 107 d.setTime(12345); 108 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 109 d.setTime(12340); 110 System.out.println(fdf.format(d) + "\t" + sdf.format(d)); 111 112 final int reps = 100000; 113 { 114 long start = System.currentTimeMillis(); 115 for (int i = 0; i < reps; i++) { 116 d.setTime(System.currentTimeMillis()); 117 fdf.format(d); 118 } 119 long elap = System.currentTimeMillis() - start; 120 System.out.println("fast: " + elap + " elapsed"); 121 System.out.println(fdf.format(d)); 122 } 123 { 124 long start = System.currentTimeMillis(); 125 for (int i = 0; i < reps; i++) { 126 d.setTime(System.currentTimeMillis()); 127 sdf.format(d); 128 } 129 long elap = System.currentTimeMillis() - start; 130 System.out.println("slow: " + elap + " elapsed"); 131 System.out.println(sdf.format(d)); 132 } 133 } 134 } 135 | Popular Tags |