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  import java.text.DateFormatSymbols;
    26
    27  /***
    28     Formats a {@link Date} in the format "dd MMM yyyy HH:mm:ss,SSS" for example,
    29     "06 Nov 1994 15:49:37,459".
    30
    31     @author Ceki G&uuml;lc&uuml;
    32     @since 0.7.5
    33  */
    34  public class DateTimeDateFormat extends AbsoluteTimeDateFormat {
    35    private static final long serialVersionUID = 5547637772208514971L;
    36
    37    String[] shortMonths;
    38
    39    public
    40    DateTimeDateFormat() {
    41      super();
    42      shortMonths = new DateFormatSymbols().getShortMonths();
    43    }
    44
    45    public
    46    DateTimeDateFormat(TimeZone timeZone) {
    47      this();
    48      setCalendar(Calendar.getInstance(timeZone));
    49    }
    50
    51    /***
    52       Appends to <code>sbuf</code> the date in the format "dd MMM yyyy
    53       HH:mm:ss,SSS" for example, "06 Nov 1994 08:49:37,459".
    54
    55       @param sbuf the string buffer to write to
    56    */
    57    public
    58    StringBuffer format(Date date, StringBuffer sbuf,
    59  		      FieldPosition fieldPosition) {
    60
    61      calendar.setTime(date);
    62
    63      int day = calendar.get(Calendar.DAY_OF_MONTH);
    64      if(day < 10)
    65        sbuf.append('0');
    66      sbuf.append(day);
    67      sbuf.append(' ');
    68      sbuf.append(shortMonths[calendar.get(Calendar.MONTH)]);
    69      sbuf.append(' ');
    70
    71      int year =  calendar.get(Calendar.YEAR);
    72      sbuf.append(year);
    73      sbuf.append(' ');
    74
    75      return super.format(date, sbuf, fieldPosition);
    76    }
    77
    78    /***
    79       This method does not do anything but return <code>null</code>.
    80     */
    81    public
    82    Date parse(java.lang.String s, ParsePosition pos) {
    83      return null;
    84    }
    85  }