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.jasper.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:
038: private DateFormat df;
039: private long lastSec = -1;
040: private StringBuffer sb = new StringBuffer();
041: private FieldPosition fp = new FieldPosition(
042: DateFormat.MILLISECOND_FIELD);
043:
044: public FastDateFormat(DateFormat df) {
045: this .df = df;
046: }
047:
048: public Date parse(String text, ParsePosition pos) {
049: return df.parse(text, pos);
050: }
051:
052: /**
053: * Note: breaks functionality of fieldPosition param. Also:
054: * there's a bug in SimpleDateFormat with "S" and "SS", use "SSS"
055: * instead if you want a msec field.
056: */
057: public StringBuffer format(Date date, StringBuffer toAppendTo,
058: FieldPosition fieldPosition) {
059: long dt = date.getTime();
060: long ds = dt / 1000;
061: if (ds != lastSec) {
062: sb.setLength(0);
063: df.format(date, sb, fp);
064: lastSec = ds;
065: } else {
066: // munge current msec into existing string
067: int ms = (int) (dt % 1000);
068: int pos = fp.getEndIndex();
069: int begin = fp.getBeginIndex();
070: if (pos > 0) {
071: if (pos > begin)
072: sb
073: .setCharAt(--pos, Character.forDigit(
074: ms % 10, 10));
075: ms /= 10;
076: if (pos > begin)
077: sb
078: .setCharAt(--pos, Character.forDigit(
079: ms % 10, 10));
080: ms /= 10;
081: if (pos > begin)
082: sb
083: .setCharAt(--pos, Character.forDigit(
084: ms % 10, 10));
085: }
086: }
087: toAppendTo.append(sb.toString());
088: return toAppendTo;
089: }
090:
091: public static void main(String[] args) {
092: String format = "yyyy-MM-dd HH:mm:ss.SSS";
093: if (args.length > 0)
094: format = args[0];
095: SimpleDateFormat sdf = new SimpleDateFormat(format);
096: FastDateFormat fdf = new FastDateFormat(sdf);
097: Date d = new Date();
098:
099: d.setTime(1);
100: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
101: d.setTime(20);
102: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
103: d.setTime(500);
104: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
105: d.setTime(543);
106: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
107: d.setTime(999);
108: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
109: d.setTime(1050);
110: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
111: d.setTime(2543);
112: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
113: d.setTime(12345);
114: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
115: d.setTime(12340);
116: System.out.println(fdf.format(d) + "\t" + sdf.format(d));
117:
118: final int reps = 100000;
119: {
120: long start = System.currentTimeMillis();
121: for (int i = 0; i < reps; i++) {
122: d.setTime(System.currentTimeMillis());
123: fdf.format(d);
124: }
125: long elap = System.currentTimeMillis() - start;
126: System.out.println("fast: " + elap + " elapsed");
127: System.out.println(fdf.format(d));
128: }
129: {
130: long start = System.currentTimeMillis();
131: for (int i = 0; i < reps; i++) {
132: d.setTime(System.currentTimeMillis());
133: sdf.format(d);
134: }
135: long elap = System.currentTimeMillis() - start;
136: System.out.println("slow: " + elap + " elapsed");
137: System.out.println(sdf.format(d));
138: }
139: }
140: }
|