1 package org.kuali.student.r2.common.util.date;
2
3 import org.joda.time.DateTime;
4 import org.joda.time.format.DateTimeFormat;
5 import org.joda.time.format.DateTimeFormatter;
6
7 import java.util.Date;
8
9
10
11
12
13
14
15
16
17 public class KSDateTimeFormatter {
18
19 protected DateTimeFormatter formatter;
20 protected String pattern;
21
22
23
24
25
26
27 public KSDateTimeFormatter(String pattern) {
28 try{
29 this.pattern = pattern;
30
31 this.formatter = DateTimeFormat.forPattern(pattern);
32 }
33 catch (IllegalArgumentException ex){
34
35 throw new IllegalArgumentException("Illegal pattern cannot be parsed. pattern["+ pattern +"].");
36 }
37
38 }
39
40
41
42
43
44
45
46
47
48
49 public Date parse(String stringDate){
50 Date dRet = null;
51
52 try{
53 DateTime dt = formatter.parseDateTime(stringDate);
54 dRet = dt.toDate();
55 }catch (IllegalArgumentException ex){
56 dRet = null;
57 throw new IllegalArgumentException(stringDate + " cannot be parsed with pattern["+ this.pattern +"].");
58 }
59
60 return dRet;
61 }
62
63
64
65
66
67
68
69 public String format(Date javaDate){
70 return this.formatter.print(new DateTime(javaDate));
71 }
72
73
74
75
76
77
78 public String format(DateTime dateTime) {
79 return this.formatter.print(dateTime);
80 }
81
82
83
84
85
86
87
88 public String format(String strDate){
89 return this.formatter.print(new DateTime(strDate));
90 }
91
92
93
94
95
96
97 public DateTimeFormatter getFormatter(){
98 return formatter;
99 }
100
101
102
103
104
105
106 public String getPattern() {
107 return pattern;
108 }
109 }