KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > util > FastDateFormat


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.util;
19
20 import java.util.Date JavaDoc;
21
22 import java.text.DateFormat JavaDoc;
23 import java.text.FieldPosition JavaDoc;
24 import java.text.ParsePosition JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26
27 /**
28  * Fast date formatter that caches recently formatted date information
29  * and uses it to avoid too-frequent calls to the underlying
30  * formatter. Note: breaks fieldPosition param of format(Date,
31  * StringBuffer, FieldPosition). If you care about the field
32  * position, call the underlying DateFormat directly.
33  *
34  * @author Stan Bailes
35  * @author Alex Chaffee
36  */

37 public class FastDateFormat extends DateFormat JavaDoc {
38
39     private DateFormat JavaDoc df;
40     private long lastSec = -1;
41     private StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
42     private FieldPosition JavaDoc fp = new FieldPosition JavaDoc(DateFormat.MILLISECOND_FIELD);
43     
44     public FastDateFormat(DateFormat JavaDoc df) {
45         this.df = df;
46     }
47
48     public Date JavaDoc parse(String JavaDoc text, ParsePosition JavaDoc pos) {
49     return df.parse(text, pos);
50     }
51
52     /**
53      * Note: breaks functionality of fieldPosition param. Also:
54      * there's a bug in SimpleDateFormat with "S" and "SS", use "SSS"
55      * instead if you want a msec field.
56      */

57     public StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc toAppendTo,
58                    FieldPosition JavaDoc 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         // munge current msec into existing string
67
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 JavaDoc[] args) {
86     String JavaDoc format = "yyyy-MM-dd HH:mm:ss.SSS";
87     if (args.length > 0)
88         format = args[0];
89         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
90         FastDateFormat fdf = new FastDateFormat(sdf);
91         Date JavaDoc d = new Date JavaDoc();
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