KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > 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.catalina.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     DateFormat JavaDoc df;
39     long lastSec = -1;
40     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
41     FieldPosition JavaDoc fp = new FieldPosition JavaDoc(DateFormat.MILLISECOND_FIELD);
42
43     public FastDateFormat(DateFormat JavaDoc df) {
44         this.df = df;
45     }
46
47     public Date JavaDoc parse(String JavaDoc text, ParsePosition JavaDoc pos) {
48         return df.parse(text, pos);
49     }
50
51     /**
52      * Note: breaks functionality of fieldPosition param. Also:
53      * there's a bug in SimpleDateFormat with "S" and "SS", use "SSS"
54      * instead if you want a msec field.
55      **/

56     public StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc toAppendTo,
57                                FieldPosition JavaDoc fieldPosition) {
58         long dt = date.getTime();
59         long ds = dt / 1000;
60         if (ds != lastSec) {
61             sb.setLength(0);
62             df.format(date, sb, fp);
63             lastSec = ds;
64         } else {
65             // munge current msec into existing string
66
int ms = (int)(dt % 1000);
67             int pos = fp.getEndIndex();
68             int begin = fp.getBeginIndex();
69             if (pos > 0) {
70                 if (pos > begin)
71                     sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
72                 ms /= 10;
73                 if (pos > begin)
74                     sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
75                 ms /= 10;
76                 if (pos > begin)
77                     sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
78             }
79         }
80         toAppendTo.append(sb.toString());
81         return toAppendTo;
82     }
83
84     public static void main(String JavaDoc[] args) {
85         String JavaDoc format = "yyyy-MM-dd HH:mm:ss.SSS";
86         if (args.length > 0)
87             format = args[0];
88         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
89         FastDateFormat fdf = new FastDateFormat(sdf);
90         Date JavaDoc d = new Date JavaDoc();
91
92         d.setTime(1); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
93         d.setTime(20); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
94         d.setTime(500); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
95         d.setTime(543); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
96         d.setTime(999); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
97         d.setTime(1050); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
98         d.setTime(2543); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
99         d.setTime(12345); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
100         d.setTime(12340); System.out.println(fdf.format(d) + "\t" + sdf.format(d));
101
102         final int reps = 100000;
103         {
104             long start = System.currentTimeMillis();
105             for (int i = 0; i < reps; i++) {
106                 d.setTime(System.currentTimeMillis());
107                 fdf.format(d);
108             }
109             long elap = System.currentTimeMillis() - start;
110             System.out.println("fast: " + elap + " elapsed");
111             System.out.println(fdf.format(d));
112         }
113         {
114             long start = System.currentTimeMillis();
115             for (int i = 0; i < reps; i++) {
116                 d.setTime(System.currentTimeMillis());
117                 sdf.format(d);
118             }
119             long elap = System.currentTimeMillis() - start;
120             System.out.println("slow: " + elap + " elapsed");
121             System.out.println(sdf.format(d));
122         }
123     }
124 }
125
Popular Tags