001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020import java.util.Calendar;
021import java.util.TimeZone;
022import java.util.Date;
023import java.text.FieldPosition;
024import java.text.ParsePosition;
025import java.text.DateFormatSymbols;
026
027/**
028   Formats a {@link Date} in the format "dd MMM yyyy HH:mm:ss,SSS" for example,
029   "06 Nov 1994 15:49:37,459".
030
031   @author Ceki Gülcü
032   @since 0.7.5
033*/
034public class DateTimeDateFormat extends AbsoluteTimeDateFormat {
035  private static final long serialVersionUID = 5547637772208514971L;
036
037  String[] shortMonths;
038
039  public
040  DateTimeDateFormat() {
041    super();
042    shortMonths = new DateFormatSymbols().getShortMonths();
043  }
044
045  public
046  DateTimeDateFormat(TimeZone timeZone) {
047    this();
048    setCalendar(Calendar.getInstance(timeZone));
049  }
050
051  /**
052     Appends to <code>sbuf</code> the date in the format "dd MMM yyyy
053     HH:mm:ss,SSS" for example, "06 Nov 1994 08:49:37,459".
054
055     @param sbuf the string buffer to write to
056  */
057  public
058  StringBuffer format(Date date, StringBuffer sbuf,
059                      FieldPosition fieldPosition) {
060
061    calendar.setTime(date);
062
063    int day = calendar.get(Calendar.DAY_OF_MONTH);
064    if(day < 10)
065      sbuf.append('0');
066    sbuf.append(day);
067    sbuf.append(' ');
068    sbuf.append(shortMonths[calendar.get(Calendar.MONTH)]);
069    sbuf.append(' ');
070
071    int year =  calendar.get(Calendar.YEAR);
072    sbuf.append(year);
073    sbuf.append(' ');
074
075    return super.format(date, sbuf, fieldPosition);
076  }
077
078  /**
079     This method does not do anything but return <code>null</code>.
080   */
081  public
082  Date parse(java.lang.String s, ParsePosition pos) {
083    return null;
084  }
085}