JAL-3032 adds Java 8 functionality (1/2)
[jalview.git] / srcjar2 / org / apache / log4j / helpers / ISO8601DateFormat.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 package org.apache.log4j.helpers;
19
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;
25
26 // Contributors: Arndt Schoenewald <arndt@ibm23093i821.mc.schoenewald.de>
27
28 /**
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".
31
32    <p>Refer to the <a
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.
36
37    @author Ceki G&uuml;lc&uuml;
38    @author Andrew Vajoczki
39
40    @since 0.7.5
41 */
42 public class ISO8601DateFormat extends AbsoluteTimeDateFormat {
43   private static final long serialVersionUID = -759840745298755296L;
44
45   public
46   ISO8601DateFormat() {
47   }
48
49   public
50   ISO8601DateFormat(TimeZone timeZone) {
51     super(timeZone);
52   }
53
54   static private long   lastTime;
55   static private char[] lastTimeString = new char[20];
56
57   /**
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".
60
61      @param sbuf the <code>StringBuffer</code> to write to
62   */
63   public
64   StringBuffer format(Date date, StringBuffer sbuf,
65                       FieldPosition fieldPosition) {
66
67     long now = date.getTime();
68     int millis = (int)(now % 1000);
69
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.
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 month;
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;
97       }
98       sbuf.append(month);
99
100       int day = calendar.get(Calendar.DAY_OF_MONTH);
101       if(day < 10) {
102         sbuf.append('0');
103     }
104       sbuf.append(day);
105
106       sbuf.append(' ');
107
108       int hour = calendar.get(Calendar.HOUR_OF_DAY);
109       if(hour < 10) {
110         sbuf.append('0');
111       }
112       sbuf.append(hour);
113       sbuf.append(':');
114
115       int mins = calendar.get(Calendar.MINUTE);
116       if(mins < 10) {
117         sbuf.append('0');
118       }
119       sbuf.append(mins);
120       sbuf.append(':');
121
122       int secs = calendar.get(Calendar.SECOND);
123       if(secs < 10) {
124         sbuf.append('0');
125       }
126       sbuf.append(secs);
127
128       sbuf.append(',');
129
130       // store the time string for next time to avoid recomputation
131       sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
132       lastTime = now - millis;
133     }
134     else {
135       sbuf.append(lastTimeString);
136     }
137
138
139     if (millis < 100) {
140         sbuf.append('0');
141     }
142     if (millis < 10) {
143         sbuf.append('0');
144     }
145
146     sbuf.append(millis);
147     return sbuf;
148   }
149
150   /**
151     This method does not do anything but return <code>null</code>.
152    */
153   public
154   Date parse(java.lang.String s, ParsePosition pos) {
155     return null;
156   }
157 }
158