2733 |
16 Oct 06 |
nicklas |
1 |
/** |
2733 |
16 Oct 06 |
nicklas |
$Id$ |
2733 |
16 Oct 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2733 |
16 Oct 06 |
nicklas |
5 |
|
2733 |
16 Oct 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2733 |
16 Oct 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2733 |
16 Oct 06 |
nicklas |
8 |
|
2733 |
16 Oct 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2733 |
16 Oct 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2733 |
16 Oct 06 |
nicklas |
of the License, or (at your option) any later version. |
2733 |
16 Oct 06 |
nicklas |
13 |
|
2733 |
16 Oct 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2733 |
16 Oct 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2733 |
16 Oct 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2733 |
16 Oct 06 |
nicklas |
GNU General Public License for more details. |
2733 |
16 Oct 06 |
nicklas |
18 |
|
2733 |
16 Oct 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2733 |
16 Oct 06 |
nicklas |
21 |
*/ |
2942 |
22 Nov 06 |
nicklas |
22 |
package net.sf.basedb.util.formatter; |
2733 |
16 Oct 06 |
nicklas |
23 |
|
2733 |
16 Oct 06 |
nicklas |
24 |
import java.text.DateFormat; |
2942 |
22 Nov 06 |
nicklas |
25 |
import java.text.ParseException; |
2733 |
16 Oct 06 |
nicklas |
26 |
import java.text.SimpleDateFormat; |
2733 |
16 Oct 06 |
nicklas |
27 |
import java.util.Date; |
2733 |
16 Oct 06 |
nicklas |
28 |
|
5430 |
28 Sep 10 |
nicklas |
29 |
import net.sf.basedb.core.DateFormatException; |
7646 |
13 Mar 19 |
nicklas |
30 |
import net.sf.basedb.util.excel.ExcelFormatter; |
7646 |
13 Mar 19 |
nicklas |
31 |
import net.sf.basedb.util.excel.ExcelValue; |
2942 |
22 Nov 06 |
nicklas |
32 |
|
2942 |
22 Nov 06 |
nicklas |
33 |
|
2733 |
16 Oct 06 |
nicklas |
34 |
/** |
2942 |
22 Nov 06 |
nicklas |
Format a date for output in a client application. This implementation |
2942 |
22 Nov 06 |
nicklas |
uses the {@link SimpleDateFormat} standard formatting routines. |
8115 |
13 Feb 23 |
nicklas |
<p> |
8115 |
13 Feb 23 |
nicklas |
NOTE! The SimpleDateFormat implementation is not thread-safe which may |
8115 |
13 Feb 23 |
nicklas |
have affected instances of this class before BASE 3.19.7. Since BASE 3.19.7 |
8115 |
13 Feb 23 |
nicklas |
this class has implemented synchronization and should be thread-safe. |
8115 |
13 Feb 23 |
nicklas |
But beware of the {@link #getDateFormat()} which return the underlying |
8115 |
13 Feb 23 |
nicklas |
formatter instance that is not thread-safe! |
2733 |
16 Oct 06 |
nicklas |
43 |
|
2733 |
16 Oct 06 |
nicklas |
@author nicklas |
2733 |
16 Oct 06 |
nicklas |
@version 2.0 |
2733 |
16 Oct 06 |
nicklas |
@base.modified $Date$ |
2733 |
16 Oct 06 |
nicklas |
@see java.text.SimpleDateFormat |
2733 |
16 Oct 06 |
nicklas |
48 |
*/ |
2733 |
16 Oct 06 |
nicklas |
49 |
public class DateFormatter |
7646 |
13 Mar 19 |
nicklas |
50 |
implements Formatter<Date>, ExcelFormatter<Date, Date> |
2733 |
16 Oct 06 |
nicklas |
51 |
{ |
2733 |
16 Oct 06 |
nicklas |
52 |
|
7646 |
13 Mar 19 |
nicklas |
53 |
private final DateFormat dateFormat; |
7646 |
13 Mar 19 |
nicklas |
54 |
private final String format; |
2733 |
16 Oct 06 |
nicklas |
55 |
|
2733 |
16 Oct 06 |
nicklas |
56 |
/** |
2733 |
16 Oct 06 |
nicklas |
Create a new date formatter. |
2733 |
16 Oct 06 |
nicklas |
@param dateFormat The date format to use when formatting |
2733 |
16 Oct 06 |
nicklas |
59 |
*/ |
2733 |
16 Oct 06 |
nicklas |
60 |
public DateFormatter(DateFormat dateFormat) |
2733 |
16 Oct 06 |
nicklas |
61 |
{ |
2733 |
16 Oct 06 |
nicklas |
62 |
this.dateFormat = dateFormat; |
7646 |
13 Mar 19 |
nicklas |
63 |
if (dateFormat instanceof SimpleDateFormat) |
7646 |
13 Mar 19 |
nicklas |
64 |
{ |
7646 |
13 Mar 19 |
nicklas |
65 |
this.format = ((SimpleDateFormat)dateFormat).toPattern(); |
7646 |
13 Mar 19 |
nicklas |
66 |
} |
7646 |
13 Mar 19 |
nicklas |
67 |
else |
7646 |
13 Mar 19 |
nicklas |
68 |
{ |
7646 |
13 Mar 19 |
nicklas |
69 |
this.format = null; |
7646 |
13 Mar 19 |
nicklas |
70 |
} |
2733 |
16 Oct 06 |
nicklas |
71 |
} |
2733 |
16 Oct 06 |
nicklas |
72 |
|
2733 |
16 Oct 06 |
nicklas |
73 |
/** |
2733 |
16 Oct 06 |
nicklas |
Create a new date formatter. |
2733 |
16 Oct 06 |
nicklas |
@param format The date format to use when formatting |
2733 |
16 Oct 06 |
nicklas |
@see SimpleDateFormat |
2733 |
16 Oct 06 |
nicklas |
77 |
*/ |
2733 |
16 Oct 06 |
nicklas |
78 |
public DateFormatter(String format) |
2733 |
16 Oct 06 |
nicklas |
79 |
{ |
2733 |
16 Oct 06 |
nicklas |
80 |
this.dateFormat = new SimpleDateFormat(format); |
2942 |
22 Nov 06 |
nicklas |
81 |
this.dateFormat.setLenient(true); |
2942 |
22 Nov 06 |
nicklas |
82 |
this.format = format; |
2733 |
16 Oct 06 |
nicklas |
83 |
} |
2733 |
16 Oct 06 |
nicklas |
84 |
|
2733 |
16 Oct 06 |
nicklas |
85 |
/* |
2733 |
16 Oct 06 |
nicklas |
From the Formatter interface |
2733 |
16 Oct 06 |
nicklas |
87 |
------------------------------------------- |
2733 |
16 Oct 06 |
nicklas |
88 |
*/ |
6127 |
14 Sep 12 |
nicklas |
89 |
@Override |
2733 |
16 Oct 06 |
nicklas |
90 |
public String format(Date value) |
2733 |
16 Oct 06 |
nicklas |
91 |
{ |
8115 |
13 Feb 23 |
nicklas |
92 |
if (value == null) return ""; |
8115 |
13 Feb 23 |
nicklas |
93 |
synchronized (dateFormat) |
8115 |
13 Feb 23 |
nicklas |
94 |
{ |
8115 |
13 Feb 23 |
nicklas |
95 |
return dateFormat.format(value); |
8115 |
13 Feb 23 |
nicklas |
96 |
} |
2733 |
16 Oct 06 |
nicklas |
97 |
} |
6127 |
14 Sep 12 |
nicklas |
98 |
@Override |
2942 |
22 Nov 06 |
nicklas |
99 |
public Date parseString(String value) |
2942 |
22 Nov 06 |
nicklas |
100 |
{ |
2942 |
22 Nov 06 |
nicklas |
101 |
if (value == null) return null; |
2942 |
22 Nov 06 |
nicklas |
102 |
try |
2942 |
22 Nov 06 |
nicklas |
103 |
{ |
8115 |
13 Feb 23 |
nicklas |
104 |
synchronized (dateFormat) |
8115 |
13 Feb 23 |
nicklas |
105 |
{ |
8115 |
13 Feb 23 |
nicklas |
106 |
return dateFormat.parse(value); |
8115 |
13 Feb 23 |
nicklas |
107 |
} |
2942 |
22 Nov 06 |
nicklas |
108 |
} |
2942 |
22 Nov 06 |
nicklas |
109 |
catch (ParseException ex) |
2942 |
22 Nov 06 |
nicklas |
110 |
{ |
2942 |
22 Nov 06 |
nicklas |
111 |
try |
2942 |
22 Nov 06 |
nicklas |
112 |
{ |
7514 |
02 Nov 18 |
nicklas |
113 |
return new Date(Long.parseLong(value)); |
2942 |
22 Nov 06 |
nicklas |
114 |
} |
2942 |
22 Nov 06 |
nicklas |
115 |
catch (Exception ex2) |
2942 |
22 Nov 06 |
nicklas |
116 |
{ |
5430 |
28 Sep 10 |
nicklas |
117 |
throw new DateFormatException(value + " is not a valid date for format: " + |
2942 |
22 Nov 06 |
nicklas |
118 |
format, ex); |
2942 |
22 Nov 06 |
nicklas |
119 |
} |
2942 |
22 Nov 06 |
nicklas |
120 |
} |
2942 |
22 Nov 06 |
nicklas |
121 |
} |
2733 |
16 Oct 06 |
nicklas |
122 |
// ------------------------------------------- |
2733 |
16 Oct 06 |
nicklas |
123 |
|
4426 |
29 Aug 08 |
nicklas |
124 |
/** |
4426 |
29 Aug 08 |
nicklas |
Get access to the underlying {@link DateFormat} object |
4426 |
29 Aug 08 |
nicklas |
used for formatting and parsing dates. |
4426 |
29 Aug 08 |
nicklas |
@since 2.8 |
4426 |
29 Aug 08 |
nicklas |
128 |
*/ |
4426 |
29 Aug 08 |
nicklas |
129 |
public DateFormat getDateFormat() |
4426 |
29 Aug 08 |
nicklas |
130 |
{ |
4426 |
29 Aug 08 |
nicklas |
131 |
return dateFormat; |
4426 |
29 Aug 08 |
nicklas |
132 |
} |
7646 |
13 Mar 19 |
nicklas |
133 |
|
7646 |
13 Mar 19 |
nicklas |
134 |
/** |
7646 |
13 Mar 19 |
nicklas |
@since 3.15 |
7646 |
13 Mar 19 |
nicklas |
136 |
*/ |
7646 |
13 Mar 19 |
nicklas |
137 |
@Override |
7646 |
13 Mar 19 |
nicklas |
138 |
public ExcelValue<Date> toExcelValue(Date value) |
7646 |
13 Mar 19 |
nicklas |
139 |
{ |
7646 |
13 Mar 19 |
nicklas |
140 |
return ExcelValue.asDate(value, format); |
7646 |
13 Mar 19 |
nicklas |
141 |
} |
4426 |
29 Aug 08 |
nicklas |
142 |
|
2733 |
16 Oct 06 |
nicklas |
143 |
} |