1
2
3
4
5
6
7
8
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
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
54
55 public String getDatePattern() {
56 return datePattern;
57 }
58
59
60
61
62
63
64
65
66 }