KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > jdk > format > AbsoluteTimeDateFormat


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 package org.jboss.logging.jdk.format;
17
18 import java.text.DateFormat JavaDoc;
19 import java.text.FieldPosition JavaDoc;
20 import java.text.ParsePosition JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.TimeZone JavaDoc;
23 import java.util.Date JavaDoc;
24
25 /**
26  * Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
27  * "15:49:37,459".
28  *
29  * @author Ceki Gülcü
30  * @author Andrew Vajoczki
31  * @author Scott.Stark@jboss.org
32  * @version $Revision: 1958 $
33  */

34 public class AbsoluteTimeDateFormat extends DateFormat JavaDoc
35 {
36
37    /**
38     * String constant used to specify {@link
39     * org.apache.log4j.helpers.AbsoluteTimeDateFormat} in layouts. Current
40     * value is <b>ABSOLUTE</b>.
41     */

42    public final static String JavaDoc ABS_TIME_DATE_FORMAT = "ABSOLUTE";
43
44    /**
45     * String constant used to specify {@link
46     * org.apache.log4j.helpers.DateTimeDateFormat} in layouts. Current
47     * value is <b>DATE</b>.
48     */

49    public final static String JavaDoc DATE_AND_TIME_DATE_FORMAT = "DATE";
50
51    /**
52     * String constant used to specify {@link
53     * org.apache.log4j.helpers.ISO8601DateFormat} in layouts. Current
54     * value is <b>ISO8601</b>.
55     */

56    public final static String JavaDoc ISO8601_DATE_FORMAT = "ISO8601";
57
58    public AbsoluteTimeDateFormat()
59    {
60       setCalendar(Calendar.getInstance());
61    }
62
63    public AbsoluteTimeDateFormat(TimeZone JavaDoc timeZone)
64    {
65       setCalendar(Calendar.getInstance(timeZone));
66    }
67
68    private static long previousTime;
69    private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
70

71    /**
72     * Appends to <code>sbuf</code> the time in the format
73     * "HH:mm:ss,SSS" for example, "15:49:37,459"
74     *
75     * @param date the date to format
76     * @param sbuf the string buffer to write to
77     * @param fieldPosition remains untouched
78     */

79    public StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc sbuf,
80       FieldPosition JavaDoc fieldPosition)
81    {
82
83       long now = date.getTime();
84       int millis = (int) (now % 1000);
85
86       if ((now - millis) != previousTime)
87       {
88          // We reach this point at most once per second
89
// across all threads instead of each time format()
90
// is called. This saves considerable CPU time.
91

92          calendar.setTime(date);
93
94          int start = sbuf.length();
95
96          int hour = calendar.get(Calendar.HOUR_OF_DAY);
97          if (hour < 10)
98          {
99             sbuf.append('0');
100          }
101          sbuf.append(hour);
102          sbuf.append(':');
103
104          int mins = calendar.get(Calendar.MINUTE);
105          if (mins < 10)
106          {
107             sbuf.append('0');
108          }
109          sbuf.append(mins);
110          sbuf.append(':');
111
112          int secs = calendar.get(Calendar.SECOND);
113          if (secs < 10)
114          {
115             sbuf.append('0');
116          }
117          sbuf.append(secs);
118          sbuf.append(',');
119
120          // store the time string for next time to avoid recomputation
121
sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
122
123          previousTime = now - millis;
124       }
125       else
126       {
127          sbuf.append(previousTimeWithoutMillis);
128       }
129
130
131       if (millis < 100)
132          sbuf.append('0');
133       if (millis < 10)
134          sbuf.append('0');
135
136       sbuf.append(millis);
137       return sbuf;
138    }
139
140    /**
141     This method does not do anything but return <code>null</code>.
142     */

143    public Date JavaDoc parse(String JavaDoc s, ParsePosition JavaDoc pos)
144    {
145       return null;
146    }
147 }
148
Popular Tags