View Javadoc

1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.core.rolling.helper;
12  
13  import java.text.SimpleDateFormat;
14  import java.util.Date;
15  
16  import ch.qos.logback.core.pattern.DynamicConverter;
17  
18  
19  /**
20   *
21   * @author Ceki Gücü
22   */
23  public class DateTokenConverter extends DynamicConverter {
24   
25    String datePattern;
26    SimpleDateFormat sdf;
27  
28    public DateTokenConverter() {
29    }
30  
31    public void start() {
32      this.datePattern = getFirstOption();
33      if(this.datePattern == null) {
34        this.datePattern = "yyyy-MM-dd";;
35      }
36      sdf = new SimpleDateFormat(datePattern);
37    }
38    
39    public String convert(Date date) {
40      return sdf.format(date);
41    }
42    
43    public String convert(Object o) {
44      if(o == null) {
45        throw new IllegalArgumentException("Null argument forbidden");
46      }
47      if(o instanceof Date) {
48        return convert((Date) o);
49      }
50      throw new IllegalArgumentException("Cannot convert "+o+" of type"+o.getClass().getName());
51    }
52    /**
53     * Return the date pattern.
54     */
55    public String getDatePattern() {
56      return datePattern;
57    }
58  
59    /**
60     * Set the date pattern.
61     */
62    //public void setDatePattern(String string) {
63    //  datePattern = string;
64    //}
65  
66  }