001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.util;
018:
019: import java.util.Date;
020:
021: import java.text.DateFormat;
022: import java.text.FieldPosition;
023: import java.text.ParsePosition;
024: import java.text.SimpleDateFormat;
025:
026: /**
027: * Fast date formatter that caches recently formatted date information
028: * and uses it to avoid too-frequent calls to the underlying
029: * formatter. Note: breaks fieldPosition param of format(Date,
030: * StringBuffer, FieldPosition). If you care about the field
031: * position, call the underlying DateFormat directly.
032: *
033: * @author Stan Bailes
034: * @author Alex Chaffee
035: **/
036: public class FastDateFormat extends DateFormat {
037: DateFormat df;
038: long lastSec = -1;
039: StringBuffer sb = new StringBuffer();
040: FieldPosition fp = new FieldPosition(DateFormat.MILLISECOND_FIELD);
041:
042: public FastDateFormat(DateFormat df) {
043: this .df = df;
044: }
045:
046: public Date parse(String text, ParsePosition pos) {
047: return df.parse(text, pos);
048: }
049:
050: /**
051: * Note: breaks functionality of fieldPosition param. Also:
052: * there's a bug in SimpleDateFormat with "S" and "SS", use "SSS"
053: * instead if you want a msec field.
054: **/
055: public StringBuffer format(Date date, StringBuffer toAppendTo,
056: FieldPosition fieldPosition) {
057: long dt = date.getTime();
058: long ds = dt / 1000;
059: if (ds != lastSec) {
060: sb.setLength(0);
061: df.format(date, sb, fp);
062: lastSec = ds;
063: } else {
064: // munge current msec into existing string
065: int ms = (int) (dt % 1000);
066: int pos = fp.getEndIndex();
067: int begin = fp.getBeginIndex();
068: if (pos > 0) {
069: if (pos > begin)
070: sb
071: .setCharAt(--pos, Character.forDigit(
072: ms % 10, 10));
073: ms /= 10;
074: if (pos > begin)
075: sb
076: .setCharAt(--pos, Character.forDigit(
077: ms % 10, 10));
078: ms /= 10;
079: if (pos > begin)
080: sb
081: .setCharAt(--pos, Character.forDigit(
082: ms % 10, 10));
083: }
084: }
085: toAppendTo.append(sb.toString());
086: return toAppendTo;
087: }
088:
089: public static void main(String[] args) {
090: String format = "yyyy-MM-dd HH:mm:ss.SSS";
091: if (args.length > 0)
092: format = args[0];
093: SimpleDateFormat sdf = new SimpleDateFormat(format);
094: FastDateFormat fdf = new FastDateFormat(sdf);
095: Date d = new Date();
096:
097: d.setTime(1);
098: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
099: d.setTime(20);
100: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
101: d.setTime(500);
102: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
103: d.setTime(543);
104: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
105: d.setTime(999);
106: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
107: d.setTime(1050);
108: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
109: d.setTime(2543);
110: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
111: d.setTime(12345);
112: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
113: d.setTime(12340);
114: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
115:
116: final int reps = 100000;
117: {
118: long start = System.currentTimeMillis();
119: for (int i = 0; i < reps; i++) {
120: d.setTime(System.currentTimeMillis());
121: fdf.format(d);
122: }
123: long elap = System.currentTimeMillis() - start;
124: System.out.println("fast: " + elap + " elapsed");
125: System.out.println(fdf.format(d));
126: }
127: {
128: long start = System.currentTimeMillis();
129: for (int i = 0; i < reps; i++) {
130: d.setTime(System.currentTimeMillis());
131: sdf.format(d);
132: }
133: long elap = System.currentTimeMillis() - start;
134: System.out.println("slow: " + elap + " elapsed");
135: System.out.println(sdf.format(d));
136: }
137: }
138: }
|