KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > 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
17 package org.apache.log4j.helpers;
18
19 import java.util.Date JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.TimeZone JavaDoc;
22 import java.text.FieldPosition JavaDoc;
23 import java.text.ParsePosition JavaDoc;
24 import java.text.DateFormat JavaDoc;
25
26
27 /**
28    Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
29    "15:49:37,459".
30    
31    @author Ceki Gülcü
32    @author Andrew Vajoczki
33
34    @since 0.7.5
35 */

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

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
59   AbsoluteTimeDateFormat() {
60     setCalendar(Calendar.getInstance());
61   }
62   
63   public
64   AbsoluteTimeDateFormat(TimeZone JavaDoc timeZone) {
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
80   StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc sbuf,
81               FieldPosition JavaDoc fieldPosition) {
82
83     long now = date.getTime();
84     int millis = (int)(now % 1000);
85
86     if ((now - millis) != previousTime) {
87       // We reach this point at most once per second
88
// across all threads instead of each time format()
89
// is called. This saves considerable CPU time.
90

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

139   public
140   Date JavaDoc parse(String JavaDoc s, ParsePosition JavaDoc pos) {
141     return null;
142   }
143 }
144
Popular Tags