KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > ISO8601DateFormat


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import java.util.Calendar JavaDoc;
20 import java.util.TimeZone JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.text.FieldPosition JavaDoc;
23 import java.text.ParsePosition JavaDoc;
24
25 // Contributors: Arndt Schoenewald <arndt@ibm23093i821.mc.schoenewald.de>
26

27 /**
28    Formats a {@link Date} in the format "YYYY-mm-dd HH:mm:ss,SSS" for example
29    "1999-11-27 15:49:37,459".
30
31    <p>Refer to the <a
32    href=http://www.cl.cam.ac.uk/~mgk25/iso-time.html>summary of the
33    International Standard Date and Time Notation</a> for more
34    information on this format.
35
36    @author Ceki G&uuml;lc&uuml;
37    @author Andrew Vajoczki
38
39    @since 0.7.5
40 */

41 public class ISO8601DateFormat extends AbsoluteTimeDateFormat {
42
43   public
44   ISO8601DateFormat() {
45   }
46
47   public
48   ISO8601DateFormat(TimeZone JavaDoc timeZone) {
49     super(timeZone);
50   }
51
52   static private long lastTime;
53   static private char[] lastTimeString = new char[20];
54
55   /**
56      Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
57      to <code>sbuf</code>. For example: "1999-11-27 15:49:37,459".
58
59      @param sbuf the <code>StringBuffer</code> to write to
60   */

61   public
62   StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc sbuf,
63               FieldPosition JavaDoc fieldPosition) {
64
65     long now = date.getTime();
66     int millis = (int)(now % 1000);
67
68     if ((now - millis) != lastTime) {
69       // We reach this point at most once per second
70
// across all threads instead of each time format()
71
// is called. This saves considerable CPU time.
72

73       calendar.setTime(date);
74
75       int start = sbuf.length();
76
77       int year = calendar.get(Calendar.YEAR);
78       sbuf.append(year);
79
80       String JavaDoc month;
81       switch(calendar.get(Calendar.MONTH)) {
82       case Calendar.JANUARY: month = "-01-"; break;
83       case Calendar.FEBRUARY: month = "-02-"; break;
84       case Calendar.MARCH: month = "-03-"; break;
85       case Calendar.APRIL: month = "-04-"; break;
86       case Calendar.MAY: month = "-05-"; break;
87       case Calendar.JUNE: month = "-06-"; break;
88       case Calendar.JULY: month = "-07-"; break;
89       case Calendar.AUGUST: month = "-08-"; break;
90       case Calendar.SEPTEMBER: month = "-09-"; break;
91       case Calendar.OCTOBER: month = "-10-"; break;
92       case Calendar.NOVEMBER: month = "-11-"; break;
93       case Calendar.DECEMBER: month = "-12-"; break;
94       default: month = "-NA-"; break;
95       }
96       sbuf.append(month);
97
98       int day = calendar.get(Calendar.DAY_OF_MONTH);
99       if(day < 10)
100     sbuf.append('0');
101       sbuf.append(day);
102
103       sbuf.append(' ');
104
105       int hour = calendar.get(Calendar.HOUR_OF_DAY);
106       if(hour < 10) {
107     sbuf.append('0');
108       }
109       sbuf.append(hour);
110       sbuf.append(':');
111
112       int mins = calendar.get(Calendar.MINUTE);
113       if(mins < 10) {
114     sbuf.append('0');
115       }
116       sbuf.append(mins);
117       sbuf.append(':');
118
119       int secs = calendar.get(Calendar.SECOND);
120       if(secs < 10) {
121     sbuf.append('0');
122       }
123       sbuf.append(secs);
124
125       sbuf.append(',');
126
127       // store the time string for next time to avoid recomputation
128
sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
129       lastTime = now - millis;
130     }
131     else {
132       sbuf.append(lastTimeString);
133     }
134
135
136     if (millis < 100)
137       sbuf.append('0');
138     if (millis < 10)
139       sbuf.append('0');
140
141     sbuf.append(millis);
142     return sbuf;
143   }
144
145   /**
146     This method does not do anything but return <code>null</code>.
147    */

148   public
149   Date JavaDoc parse(java.lang.String JavaDoc s, ParsePosition JavaDoc pos) {
150     return null;
151   }
152 }
153
154
Popular Tags