View Javadoc

    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) {
    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       sbuf.append(day);
    104
    105       sbuf.append(' ');
    106
    107       int hour = calendar.get(Calendar.HOUR_OF_DAY);
    108       if(hour < 10) {
    109 	sbuf.append('0');
    110       }
    111       sbuf.append(hour);
    112       sbuf.append(':');
    113
    114       int mins = calendar.get(Calendar.MINUTE);
    115       if(mins < 10) {
    116 	sbuf.append('0');
    117       }
    118       sbuf.append(mins);
    119       sbuf.append(':');
    120
    121       int secs = calendar.get(Calendar.SECOND);
    122       if(secs < 10) {
    123 	sbuf.append('0');
    124       }
    125       sbuf.append(secs);
    126
    127       sbuf.append(',');
    128
    129       // store the time string for next time to avoid recomputation
    130       sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
    131       lastTime = now - millis;
    132     }
    133     else {
    134       sbuf.append(lastTimeString);
    135     }
    136
    137
    138     if (millis < 100)
    139       sbuf.append('0');
    140     if (millis < 10)
    141       sbuf.append('0');
    142
    143     sbuf.append(millis);
    144     return sbuf;
    145   }
    146
    147   /***
    148     This method does not do anything but return <code>null</code>.
    149    */
    150   public
    151   Date parse(java.lang.String s, ParsePosition pos) {
    152     return null;
    153   }
    154 }
    155