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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.helpers;
20 import java.util.Date;
21 import java.util.Calendar;
22 import java.util.TimeZone;
23 import java.text.FieldPosition;
24 import java.text.ParsePosition;
25 import java.text.DateFormat;
29 Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
32 @author Ceki Gülcü
33 @author Andrew Vajoczki
37 public class AbsoluteTimeDateFormat extends DateFormat {
38 private static final long serialVersionUID = -388856345976723342L;
41 String constant used to specify {@link
42 org.apache.log4j.helpers.AbsoluteTimeDateFormat} in layouts. Current
43 value is <b>ABSOLUTE</b>. */
44 public final static String ABS_TIME_DATE_FORMAT = "ABSOLUTE";
47 String constant used to specify {@link
48 org.apache.log4j.helpers.DateTimeDateFormat} in layouts. Current
51 public final static String DATE_AND_TIME_DATE_FORMAT = "DATE";
54 String constant used to specify {@link
55 org.apache.log4j.helpers.ISO8601DateFormat} in layouts. Current
56 value is <b>ISO8601</b>.
58 public final static String ISO8601_DATE_FORMAT = "ISO8601";
61 AbsoluteTimeDateFormat() {
62 setCalendar(Calendar.getInstance());
66 AbsoluteTimeDateFormat(TimeZone timeZone) {
67 setCalendar(Calendar.getInstance(timeZone));
70 private static long previousTime;
71 private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
74 Appends to <code>sbuf</code> the time in the format
75 "HH:mm:ss,SSS" for example, "15:49:37,459"
77 @param date the date to format
78 @param sbuf the string buffer to write to
79 @param fieldPosition remains untouched
82 StringBuffer format(Date date, StringBuffer sbuf,
83 FieldPosition fieldPosition) {
85 long now = date.getTime();
86 int millis = (int)(now % 1000);
88 if ((now - millis) != previousTime || previousTimeWithoutMillis[0] == 0) {
89 // We reach this point at most once per second
90 // across all threads instead of each time format()
91 // is called. This saves considerable CPU time.
93 calendar.setTime(date);
95 int start = sbuf.length();
97 int hour = calendar.get(Calendar.HOUR_OF_DAY);
104 int mins = calendar.get(Calendar.MINUTE);
111 int secs = calendar.get(Calendar.SECOND);
118 // store the time string for next time to avoid recomputation
119 sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
121 previousTime = now - millis;
124 sbuf.append(previousTimeWithoutMillis);
141 This method does not do anything but return <code>null</code>.
144 Date parse(String s, ParsePosition pos) {