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.Calendar;
21 import java.util.TimeZone;
22 import java.util.Date;
23 import java.text.FieldPosition;
24 import java.text.ParsePosition;
26 // Contributors: Arndt Schoenewald <arndt@ibm23093i821.mc.schoenewald.de>
29 Formats a {@link Date} in the format "yyyy-MM-dd HH:mm:ss,SSS" for example
30 "1999-11-27 15:49:37,459".
33 href=http://www.cl.cam.ac.uk/~mgk25/iso-time.html>summary of the
34 International Standard Date and Time Notation</a> for more
35 information on this format.
37 @author Ceki Gülcü
38 @author Andrew Vajoczki
42 public class ISO8601DateFormat extends AbsoluteTimeDateFormat {
43 private static final long serialVersionUID = -759840745298755296L;
50 ISO8601DateFormat(TimeZone timeZone) {
54 static private long lastTime;
55 static private char[] lastTimeString = new char[20];
58 Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
59 to <code>sbuf</code>. For example: "1999-11-27 15:49:37,459".
61 @param sbuf the <code>StringBuffer</code> to write to
64 StringBuffer format(Date date, StringBuffer sbuf,
65 FieldPosition fieldPosition) {
67 long now = date.getTime();
68 int millis = (int)(now % 1000);
70 if ((now - millis) != lastTime || lastTimeString[0] == 0) {
71 // We reach this point at most once per second
72 // across all threads instead of each time format()
73 // is called. This saves considerable CPU time.
75 calendar.setTime(date);
77 int start = sbuf.length();
79 int year = calendar.get(Calendar.YEAR);
83 switch(calendar.get(Calendar.MONTH)) {
84 case Calendar.JANUARY: month = "-01-"; break;
85 case Calendar.FEBRUARY: month = "-02-"; break;
86 case Calendar.MARCH: month = "-03-"; break;
87 case Calendar.APRIL: month = "-04-"; break;
88 case Calendar.MAY: month = "-05-"; break;
89 case Calendar.JUNE: month = "-06-"; break;
90 case Calendar.JULY: month = "-07-"; break;
91 case Calendar.AUGUST: month = "-08-"; break;
92 case Calendar.SEPTEMBER: month = "-09-"; break;
93 case Calendar.OCTOBER: month = "-10-"; break;
94 case Calendar.NOVEMBER: month = "-11-"; break;
95 case Calendar.DECEMBER: month = "-12-"; break;
96 default: month = "-NA-"; break;
100 int day = calendar.get(Calendar.DAY_OF_MONTH);
108 int hour = calendar.get(Calendar.HOUR_OF_DAY);
115 int mins = calendar.get(Calendar.MINUTE);
122 int secs = calendar.get(Calendar.SECOND);
130 // store the time string for next time to avoid recomputation
131 sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
132 lastTime = now - millis;
135 sbuf.append(lastTimeString);
151 This method does not do anything but return <code>null</code>.
154 Date parse(java.lang.String s, ParsePosition pos) {