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
24
25
26
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
38
39
40
41 public JaretDateFormatter(Locale locale) {
42 setLocale(locale);
43 }
44
45
46
47
48 public JaretDateFormatter() {
49 this(Locale.getDefault());
50 }
51
52
53
54
55
56
57 public Locale getLocale() {
58 return _locale;
59 }
60
61
62
63
64
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
86
87 public boolean equals(Object obj) {
88 JaretDateFormatter jdf = (JaretDateFormatter) obj;
89
90 return jdf.getLocale().equals(_locale);
91 }
92
93
94
95
96 public int hashCode() {
97
98
99 return _locale.hashCode();
100 }
101
102
103
104
105
106
107
108 public String generateDisplayString(JaretDate date) {
109 return _df.format(date.getDate());
110 }
111
112
113
114
115
116
117
118 public String generateDisplayStringDate(JaretDate date) {
119 return _df2.format(date.getDate());
120 }
121
122
123
124
125
126
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
139
140
141
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
173
174
175
176
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 }