1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.date;
12
13 import java.text.DateFormat;
14 import java.text.DateFormatSymbols;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18 import java.util.Locale;
19
20 import de.jaret.util.misc.FormatHelper;
21
22 /***
23 * Delegate for the JaretDate class supplying convenient methods for generating formatted, i8ned output.
24 *
25 * @author Peter Kliem
26 * @version $Id: JaretDateFormatter.java 243 2007-02-11 22:08:49Z olk $
27 */
28 public class JaretDateFormatter {
29 Locale _locale;
30 private DateFormat _df;
31 private DateFormat _df2;
32 private DateFormatSymbols _dateFormatSymbols;
33 private SimpleDateFormat _dfTimeSeconds;
34 private SimpleDateFormat _dfTimeNoSeconds;
35
36 /***
37 * Construcor specifying a locale.
38 *
39 * @param locale locale to be used
40 */
41 public JaretDateFormatter(Locale locale) {
42 setLocale(locale);
43 }
44
45 /***
46 * Default constructor unsing default locale.
47 */
48 public JaretDateFormatter() {
49 this(Locale.getDefault());
50 }
51
52 /***
53 * Retrieve the used locale.
54 *
55 * @return the locale set for the JaretDateFormatter
56 */
57 public Locale getLocale() {
58 return _locale;
59 }
60
61 /***
62 * Set the locale for this JaretDateFormatter.
63 *
64 * @param locale Locale to be used
65 */
66 public void setLocale(Locale locale) {
67 _locale = locale;
68 _df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, _locale);
69 _df2 = DateFormat.getDateInstance(DateFormat.SHORT, _locale);
70 _df.setLenient(false);
71 _df2.setLenient(false);
72 }
73
74 public Date parseTextualDate(String text) {
75 Date date = null;
76 try {
77 date = _df2.parse(text);
78 } catch (ParseException e) {
79
80 }
81 return date;
82 }
83
84 /***
85 * {@inheritDoc}
86 */
87 public boolean equals(Object obj) {
88 JaretDateFormatter jdf = (JaretDateFormatter) obj;
89
90 return jdf.getLocale().equals(_locale);
91 }
92
93 /***
94 * {@inheritDoc}
95 */
96 public int hashCode() {
97
98
99 return _locale.hashCode();
100 }
101
102 /***
103 * Generate a textual representation of the given JaretDate.
104 *
105 * @param date the JaretDate to be formatted
106 * @return JaretDate to format
107 */
108 public String generateDisplayString(JaretDate date) {
109 return _df.format(date.getDate());
110 }
111
112 /***
113 * Generate a textual representation of the given JaretDate.
114 *
115 * @param date the JaretDate to be formatted
116 * @return textual represantation of the date (day)
117 */
118 public String generateDisplayStringDate(JaretDate date) {
119 return _df2.format(date.getDate());
120 }
121
122 /***
123 * Generate an output of the format hh:mm:ss for a given number of seconds.
124 *
125 * @param sec Seconds
126 * @return textual representation
127 */
128 public static String secondsToDisplayString(int sec) {
129 int hours = sec / 3600;
130 int minutes = (sec % 3600) / 60;
131 int seconds = (sec % 60);
132 String str = FormatHelper.NFInt2Digits().format(hours) + ":" + FormatHelper.NFInt2Digits().format(minutes)
133 + ":" + FormatHelper.NFInt2Digits().format(seconds);
134 return str;
135 }
136
137 /***
138 * Retrieve a localized name for the day of the week.
139 *
140 * @param dayOfWeek
141 * @return the localized name of the weekday
142 */
143 public String getDayOfWeekString(int dayOfWeek) {
144 if (_dateFormatSymbols == null) {
145 _dateFormatSymbols = new DateFormatSymbols(_locale);
146 }
147 return _dateFormatSymbols.getWeekdays()[dayOfWeek];
148 }
149
150 public String getMonthString(int month) {
151 if (_dateFormatSymbols == null) {
152 _dateFormatSymbols = new DateFormatSymbols(_locale);
153 }
154 return _dateFormatSymbols.getMonths()[month];
155 }
156
157 public String getShortDayOfWeekString(int dayOfWeek) {
158 if (_dateFormatSymbols == null) {
159 _dateFormatSymbols = new DateFormatSymbols(_locale);
160 }
161 return _dateFormatSymbols.getShortWeekdays()[dayOfWeek];
162 }
163
164 public String getShortMonthString(int month) {
165 if (_dateFormatSymbols == null) {
166 _dateFormatSymbols = new DateFormatSymbols(_locale);
167 }
168 return _dateFormatSymbols.getShortMonths()[month];
169 }
170
171 /***
172 * Generates a textual representation of the time only.
173 *
174 * @param date JaretDate to be represenetd
175 * @param seconds if true seconds will be included (hh:mm:ss), hh:mm otherwise
176 * @return a Textual represenation of the time
177 */
178 public String toDisplayStringTime(JaretDate date, boolean seconds) {
179 if (_dfTimeSeconds == null) {
180 _dfTimeSeconds = new SimpleDateFormat("HH:mm:ss", _locale);
181 _dfTimeNoSeconds = new SimpleDateFormat("HH:mm", _locale);
182 }
183 if (seconds) {
184 return _dfTimeSeconds.format(date.getDate());
185 } else {
186 return _dfTimeNoSeconds.format(date.getDate());
187 }
188 }
189
190 }