KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > jdk > format > 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.jboss.logging.jdk.format;
18
19 import java.util.TimeZone JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Calendar 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 java.util.Date} in the format "YYYY-mm-dd HH:mm:ss,SSS" for example
29  * "1999-11-27 15:49:37,459".
30  * <p/>
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  * @author Scott.Stark@jboss.org
39  * @version $Revision: 1958 $
40  */

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

62    public StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc sbuf,
63       FieldPosition JavaDoc fieldPosition)
64    {
65
66       long now = date.getTime();
67       int millis = (int) (now % 1000);
68
69       if ((now - millis) != lastTime)
70       {
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.
74

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

181    public Date JavaDoc parse(java.lang.String JavaDoc s, ParsePosition JavaDoc pos)
182    {
183       return null;
184    }
185 }
186
187
Popular Tags