1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.util;
24
25 import java.text.DateFormat;
26 import java.util.Calendar;
27 import java.util.GregorianCalendar;
28 import java.util.Locale;
29
30 /***
31 * Static utility methods for date operations and conversions.
32 *
33 * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede </a>
34 */
35 public final class DateUtil {
36
37 /***
38 * Date format used for parsing.
39 * The format it compatible with the format of class <tt>java.text.SimpleDateFormat</tt>.
40 */
41 public static final String DATE_FORMAT = "yyyy-MM-dd";
42
43 /***
44 * Time format used for parsing.
45 * The format it compatible with the format of class <tt>java.text.SimpleDateFormat</tt>.
46 */
47 public static final String TIME_FORMAT = "hh:mm:ss";
48
49 /***
50 * Date/time format used for parsing.
51 * The format it compatible with the format of class <tt>java.text.SimpleDateFormat</tt>.
52 */
53 public static final String DATE_TIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
54
55 /*** Start index of year component within date format. */
56 private static final int YEAR_START = DATE_TIME_FORMAT.indexOf('y');
57
58 /*** End index of year component within date format. */
59 private static final int YEAR_END = DATE_TIME_FORMAT.lastIndexOf('y');
60
61 /*** Start index of year component within date format. */
62 private static final int MONTH_START = DATE_TIME_FORMAT.indexOf('M');
63
64 /*** End index of year component within date format. */
65 private static final int MONTH_END = DATE_TIME_FORMAT.lastIndexOf('M');
66
67 /*** Start index of year component within date format. */
68 private static final int DAY_START = DATE_TIME_FORMAT.indexOf('d');
69
70 /*** End index of year component within date format. */
71 private static final int DAY_END = DATE_TIME_FORMAT.lastIndexOf('d');
72
73 /*** Start index of year component within date format. */
74 private static final int HOUR_START = DATE_TIME_FORMAT.indexOf('h');
75
76 /*** End index of year component within date format. */
77 private static final int HOUR_END = DATE_TIME_FORMAT.lastIndexOf('h');
78
79 /*** Start index of year component within date format. */
80 private static final int MINUTE_START = DATE_TIME_FORMAT.indexOf('m');
81
82 /*** End index of year component within date format. */
83 private static final int MINUTE_END = DATE_TIME_FORMAT.lastIndexOf('m');
84
85 /*** Start index of year component within date format. */
86 private static final int SECOND_START = DATE_TIME_FORMAT.indexOf('s');
87
88 /*** End index of year component within date format. */
89 private static final int SECOND_END = DATE_TIME_FORMAT.lastIndexOf('s');
90
91 /***
92 * private constructor to prevent instantiation.
93 */
94 private DateUtil() {
95 }
96
97 /***
98 * Parses a date given as a string.
99 * The format of the resulting string is {@link DateUtil#DATE_TIME_FORMAT}.
100 *
101 * @param date a date formatted as a string
102 * @param adjustToLocalTimeZone if result should be adjusted to local time zone or not
103 * @return the date as a <tt>java.util.Calendar</tt> or <tt>null</tt> if argument string is null or empty
104 * @throws IndexOutOfBoundsException if the String is too short
105 * @throws NumberFormatException if a date/time component is out of range or cannot be parsed
106 */
107 public static Calendar parseDate(final String date, final boolean adjustToLocalTimeZone)
108 throws IndexOutOfBoundsException, NumberFormatException {
109 if (date == null || "".equals(date)) {
110 return null;
111 }
112 final int year = Integer.parseInt(date.substring(YEAR_START, YEAR_END + 1));
113 final int month = Integer.parseInt(date.substring(MONTH_START, MONTH_END + 1));
114 final int day = Integer.parseInt(date.substring(DAY_START, DAY_END + 1));
115 final int hour;
116 final int minute;
117 final int second;
118 if (date.length() > HOUR_START) {
119 hour = Integer.parseInt(date.substring(HOUR_START, HOUR_END + 1));
120 minute = Integer.parseInt(date.substring(MINUTE_START, MINUTE_END + 1));
121 second = Integer.parseInt(date.substring(SECOND_START, SECOND_END + 1));
122 } else {
123 hour = 0;
124 minute = 0;
125 second = 0;
126 }
127 final Calendar calendar = new GregorianCalendar(year, month - 1, day, hour, minute, second);
128 if (adjustToLocalTimeZone) {
129 calendar.add(Calendar.MILLISECOND, Calendar.getInstance().getTimeZone().getRawOffset());
130 }
131 return calendar;
132 }
133
134 /***
135 * Formats the date and time time of a Calendar instance to a String.
136 * The format of the resulting string is the short date/time format of the default locale.
137 *
138 * @param date the date as a <tt>java.util.Calendar</tt>
139 * @param time the time as a <tt>java.util.Calendar</tt>
140 * @return date as a formatted string
141 */
142 public static String getDateTimeString(final Calendar date, final Calendar time) {
143 return getDateTimeString(date, time, Locale.getDefault());
144 }
145
146 /***
147 * Formats the date and time time of a Calendar instance to a String.
148 * The format of the resulting string is the short date/time format of the given locale.
149 *
150 * @param date the date as a <tt>java.util.Calendar</tt>
151 * @param time the time as a <tt>java.util.Calendar</tt>
152 * @param locale locale
153 * @return date as a formatted string
154 */
155 public static String getDateTimeString(final Calendar date, final Calendar time, final Locale locale) {
156 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
157 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.LONG, locale);
158 return dateFormat.format(date.getTime()) + " " + timeFormat.format(time.getTime());
159 }
160
161 /***
162 * Formats the date and time time of a Calendar instance to a String.
163 * The format of the resulting string is the short date/time format of the default locale.
164 *
165 * @param calendar the date as a <tt>java.util.Calendar</tt>
166 * @return date as a formatted string
167 */
168 public static String getDateTimeString(final Calendar calendar) {
169 return getDateTimeString(calendar, Locale.getDefault());
170 }
171
172 /***
173 * Formats the date and time time of a Calendar instance to a String.
174 * The format of the resulting string is the short date/time format of the given locale.
175 *
176 * @param calendar the date as a <tt>java.util.Calendar</tt>
177 * @param locale locale
178 * @return date as a formatted string
179 */
180 public static String getDateTimeString(final Calendar calendar, final Locale locale) {
181 DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
182 return format.format(calendar.getTime());
183 }
184
185 /***
186 * Formats the date of a Calendar instance to a String.
187 * The format of the resulting string is the short date/time format of the defaultlocale.
188 *
189 * @param calendar the date as a <tt>java.util.Calendar</tt>
190 * @return date as a formatted string
191 */
192 public static String getDateString(final Calendar calendar) {
193 return getDateString(calendar, Locale.getDefault());
194 }
195
196 /***
197 * Formats the date of a Calendar instance to a String.
198 * The format of the resulting string is the short date/time format of the given locale.
199 *
200 * @param calendar the date as a <tt>java.util.Calendar</tt>
201 * @param locale locale
202 * @return date as a formatted string
203 */
204 public static String getDateString(final Calendar calendar, final Locale locale) {
205 DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
206 return format.format(calendar.getTime());
207 }
208
209 /***
210 * Formats the time of a Calendar instance to a String.
211 * The format of the resulting string is the short time format of the default locale.
212 *
213 * @param calendar the date as a <tt>java.util.Calendar</tt>
214 * @return date as a formatted string
215 */
216 public static String getTimeString(final Calendar calendar) {
217 return getTimeString(calendar, Locale.getDefault());
218 }
219
220 /***
221 * Formats the time of a Calendar instance to a String.
222 * The format of the resulting string is the short time format of the given locale.
223 *
224 * @param calendar the date as a <tt>java.util.Calendar</tt>
225 * @param locale locale
226 * @return date as a formatted string
227 */
228 public static String getTimeString(final Calendar calendar, final Locale locale) {
229 DateFormat format = DateFormat.getTimeInstance(DateFormat.LONG, locale);
230 return format.format(calendar.getTime());
231 }
232 }