FileDocCategorySizeDatePackage
DateLayout.javaAPI DocApache log4j 1.2.156448Sat Aug 25 00:09:40 BST 2007org.apache.log4j.helpers

DateLayout

public abstract class DateLayout extends Layout
This abstract layout takes care of all the date related options and formatting work.
author
Ceki Gülcü

Fields Summary
public static final String
NULL_DATE_FORMAT
String constant designating no time information. Current value of this constant is NULL.
public static final String
RELATIVE_TIME_DATE_FORMAT
String constant designating relative time. Current value of this constant is RELATIVE.
protected FieldPosition
pos
public static final String
DATE_FORMAT_OPTION
public static final String
TIMEZONE_OPTION
private String
timeZoneID
private String
dateFormatOption
protected DateFormat
dateFormat
protected Date
date
Constructors Summary
Methods Summary
public voidactivateOptions()

    setDateFormat(dateFormatOption);
    if(timeZoneID != null && dateFormat != null) {
      dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
    }
  
public voiddateFormat(java.lang.StringBuffer buf, org.apache.log4j.spi.LoggingEvent event)

    if(dateFormat != null) {
      date.setTime(event.timeStamp);
      dateFormat.format(date, buf, this.pos);
      buf.append(' ");
    }
  
public java.lang.StringgetDateFormat()
Returns value of the DateFormat option.

    return dateFormatOption;
  
public java.lang.String[]getOptionStrings()

deprecated
Use the setter method for the option directly instead of the generic setOption method.


                            
  
    
    return new String[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
  
public java.lang.StringgetTimeZone()
Returns value of the TimeZone option.

    return timeZoneID;
  
public voidsetDateFormat(java.lang.String dateFormatType, java.util.TimeZone timeZone)
Sets the DateFormat used to format date and time in the time zone determined by timeZone parameter. The {@link DateFormat} used will depend on the dateFormatType.

The recognized types are {@link #NULL_DATE_FORMAT}, {@link #RELATIVE_TIME_DATE_FORMAT} {@link AbsoluteTimeDateFormat#ABS_TIME_DATE_FORMAT}, {@link AbsoluteTimeDateFormat#DATE_AND_TIME_DATE_FORMAT} and {@link AbsoluteTimeDateFormat#ISO8601_DATE_FORMAT}. If the dateFormatType is not one of the above, then the argument is assumed to be a date pattern for {@link SimpleDateFormat}.

    if(dateFormatType == null) {
      this.dateFormat = null;
      return;
    } 

    if(dateFormatType.equalsIgnoreCase(NULL_DATE_FORMAT)) {
      this.dateFormat = null;
    } else if (dateFormatType.equalsIgnoreCase(RELATIVE_TIME_DATE_FORMAT)) {
      this.dateFormat =  new RelativeTimeDateFormat();
    } else if(dateFormatType.equalsIgnoreCase(
                             AbsoluteTimeDateFormat.ABS_TIME_DATE_FORMAT)) {
      this.dateFormat =  new AbsoluteTimeDateFormat(timeZone);
    } else if(dateFormatType.equalsIgnoreCase(
                        AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT)) {
      this.dateFormat =  new DateTimeDateFormat(timeZone);
    } else if(dateFormatType.equalsIgnoreCase(
                              AbsoluteTimeDateFormat.ISO8601_DATE_FORMAT)) {
      this.dateFormat =  new ISO8601DateFormat(timeZone);
    } else {
      this.dateFormat = new SimpleDateFormat(dateFormatType);
      this.dateFormat.setTimeZone(timeZone);
    }
  
public voidsetDateFormat(java.lang.String dateFormat)
The value of the DateFormat option should be either an argument to the constructor of {@link SimpleDateFormat} or one of the srings "NULL", "RELATIVE", "ABSOLUTE", "DATE" or "ISO8601.

    if (dateFormat != null) {
        dateFormatOption = dateFormat;
    }
    setDateFormat(dateFormatOption, TimeZone.getDefault());
  
public voidsetDateFormat(java.text.DateFormat dateFormat, java.util.TimeZone timeZone)
Sets the {@link DateFormat} used to format time and date in the zone determined by timeZone.

    this.dateFormat = dateFormat;    
    this.dateFormat.setTimeZone(timeZone);
  
public voidsetOption(java.lang.String option, java.lang.String value)

deprecated
Use the setter method for the option directly instead of the generic setOption method.

    if(option.equalsIgnoreCase(DATE_FORMAT_OPTION)) {
      dateFormatOption = value.toUpperCase();
    } else if(option.equalsIgnoreCase(TIMEZONE_OPTION)) {
      timeZoneID = value;
    }
  
public voidsetTimeZone(java.lang.String timeZone)
The TimeZoneID option is a time zone ID string in the format expected by the {@link TimeZone#getTimeZone} method.

    this.timeZoneID = timeZone;