1333 |
05 Apr 11 |
nicklas |
1 |
package net.sf.basedb.reggie.converter; |
1333 |
05 Apr 11 |
nicklas |
2 |
|
1333 |
05 Apr 11 |
nicklas |
3 |
import java.text.DateFormat; |
3958 |
20 May 16 |
nicklas |
4 |
import java.text.ParseException; |
1333 |
05 Apr 11 |
nicklas |
5 |
import java.util.Date; |
1333 |
05 Apr 11 |
nicklas |
6 |
|
7024 |
07 Feb 23 |
nicklas |
7 |
import org.apache.commons.lang3.time.FastDateFormat; |
7024 |
07 Feb 23 |
nicklas |
8 |
|
3958 |
20 May 16 |
nicklas |
9 |
import net.sf.basedb.util.formatter.Formatter; |
3958 |
20 May 16 |
nicklas |
10 |
|
1333 |
05 Apr 11 |
nicklas |
11 |
/** |
1333 |
05 Apr 11 |
nicklas |
Value converter implementation for converting dates to string representation. |
7024 |
07 Feb 23 |
nicklas |
The instance is configured with a {@link FastDateFormat} object which does the |
1333 |
05 Apr 11 |
nicklas |
actual conversion. See {@link DateFormat#format(Date)}. |
1333 |
05 Apr 11 |
nicklas |
@author nicklas |
7024 |
07 Feb 23 |
nicklas |
@since 1.2, 4.44 |
1333 |
05 Apr 11 |
nicklas |
17 |
*/ |
1333 |
05 Apr 11 |
nicklas |
18 |
public class DateToStringConverter |
3958 |
20 May 16 |
nicklas |
19 |
implements ValueConverter<Date, String>, Formatter<Date> |
1333 |
05 Apr 11 |
nicklas |
20 |
{ |
1333 |
05 Apr 11 |
nicklas |
21 |
|
7024 |
07 Feb 23 |
nicklas |
22 |
private final FastDateFormat fastDateFormat; |
1333 |
05 Apr 11 |
nicklas |
23 |
|
1333 |
05 Apr 11 |
nicklas |
24 |
/** |
1333 |
05 Apr 11 |
nicklas |
Create a new date-to-string converter. |
1333 |
05 Apr 11 |
nicklas |
@param dateFormat The formatter object used for the actual conversion |
1333 |
05 Apr 11 |
nicklas |
27 |
*/ |
7024 |
07 Feb 23 |
nicklas |
28 |
public DateToStringConverter(FastDateFormat dateFormat) |
1333 |
05 Apr 11 |
nicklas |
29 |
{ |
1333 |
05 Apr 11 |
nicklas |
30 |
if (dateFormat == null) throw new NullPointerException("dateFormat"); |
7024 |
07 Feb 23 |
nicklas |
31 |
this.fastDateFormat = dateFormat; |
1333 |
05 Apr 11 |
nicklas |
32 |
} |
7024 |
07 Feb 23 |
nicklas |
33 |
|
1333 |
05 Apr 11 |
nicklas |
34 |
|
1333 |
05 Apr 11 |
nicklas |
35 |
/* |
1333 |
05 Apr 11 |
nicklas |
From the ValueConverter interface |
1333 |
05 Apr 11 |
nicklas |
37 |
--------------------------------- |
1333 |
05 Apr 11 |
nicklas |
38 |
*/ |
1333 |
05 Apr 11 |
nicklas |
39 |
@Override |
1333 |
05 Apr 11 |
nicklas |
40 |
public String convert(Date value) |
1333 |
05 Apr 11 |
nicklas |
41 |
{ |
7024 |
07 Feb 23 |
nicklas |
42 |
return value == null ? null : fastDateFormat.format(value); |
1333 |
05 Apr 11 |
nicklas |
43 |
} |
1333 |
05 Apr 11 |
nicklas |
44 |
// ---------------------------------- |
3958 |
20 May 16 |
nicklas |
45 |
|
3958 |
20 May 16 |
nicklas |
46 |
@Override |
3958 |
20 May 16 |
nicklas |
47 |
public String format(Date value) |
3958 |
20 May 16 |
nicklas |
48 |
{ |
3958 |
20 May 16 |
nicklas |
49 |
return convert(value); |
3958 |
20 May 16 |
nicklas |
50 |
} |
3958 |
20 May 16 |
nicklas |
51 |
|
3958 |
20 May 16 |
nicklas |
52 |
@Override |
3958 |
20 May 16 |
nicklas |
53 |
public Date parseString(String value) |
3958 |
20 May 16 |
nicklas |
54 |
{ |
3958 |
20 May 16 |
nicklas |
55 |
try |
3958 |
20 May 16 |
nicklas |
56 |
{ |
7024 |
07 Feb 23 |
nicklas |
57 |
return fastDateFormat.parse(value); |
3958 |
20 May 16 |
nicklas |
58 |
} |
3958 |
20 May 16 |
nicklas |
59 |
catch (ParseException ex) |
3958 |
20 May 16 |
nicklas |
60 |
{ |
3958 |
20 May 16 |
nicklas |
61 |
throw new RuntimeException("Could not parse date: " + value, ex); |
3958 |
20 May 16 |
nicklas |
62 |
} |
3958 |
20 May 16 |
nicklas |
63 |
} |
1333 |
05 Apr 11 |
nicklas |
64 |
} |