1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.util.codingsupport;
18
19 import java.util.Locale;
20
21 import org.joda.time.DateTime;
22 import org.joda.time.LocalDate;
23 import org.joda.time.LocalTime;
24 import org.joda.time.format.DateTimeFormat;
25 import org.joda.time.format.DateTimeFormatter;
26
27 /**
28 *
29 * This class is a class providing static mehtods for conversion between JodaTime formats and strings.
30 *
31 * @svnLink $Revision: 3966 $;$Date: 2009-10-22 09:07:47 +0200 (Do, 22. Okt 2009) $;$Author: dajamesthomas $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/codingsupport/JodaTimeUtils.java $
32 *
33 * @author Daniel Thomas (DTH)
34 */
35 public class JodaTimeUtils {
36
37
38 /**
39 * dd.MM.YYYY
40 */
41 public static final String DEFAULT_DATE_WITH_PERIOD = "dd.MM.YYYY";
42
43 /**
44 * dd:MM:YYYY
45 */
46 public static final String DEFAULT_DATE_WITH_COLON = "dd:MM:YYYY";
47
48 /**
49 * dd MM YYYY
50 */
51 public static final String DEFAULT_DATE_WITH_WHITESPACE = "dd MM YYYY";
52
53
54 /**
55 * HH.mm.ss
56 */
57 public static final String TIME_WITH_PERIOD = "HH.mm.ss";
58
59 /**
60 * HH:mm:ss
61 */
62 public static final String TIME_WITH_COLON = "HH:mm:ss";
63
64 /**
65 * HH mm ss
66 */
67 public static final String TIME_WITH_WHITESPACE = "HH mm ss";
68
69 /**
70 * dd.MM.YY HH:mm:ss
71 */
72 public static final String DEFAULT_DATETIME = DEFAULT_DATE_WITH_PERIOD + " " + TIME_WITH_COLON;
73
74
75
76 /**
77 * Hide default constructor.
78 */
79 protected JodaTimeUtils() {}
80
81 /**
82 * Returns a string containing the date and/or time from the DateTime.
83 * The format of the returned string is defined by the pattern.
84 * (e.g. "dd.MM.YYYY hh.mm.ss" for "26.8.2009 08.26.52")
85 *
86 * @param date is the DateTime to process
87 * @param pattern is the pattern to apply
88 * @return a string with the date/time in the specified format
89 */
90 public static String getDateTimeString(DateTime date, String pattern) {
91 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
92 return fmt.print(date);
93 }
94
95 /**
96 * Returns a string containing the date in a LocalDate.
97 * The format of the returned string is defined by the pattern.
98 * Attention: any time related characters in the pattern will be
99 * set to 0.
100 * (e.g. 'mm' in the pattern will result in a '00' as there is no time in a LocalDate.
101 *
102 * @param date is the LocalDate to process
103 * @param pattern is the pattern to apply
104 * @return a string with the date in the format specified by pattern
105 */
106 public static String getLocalDateString(LocalDate date, String pattern) {
107 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
108 return fmt.print(date);
109 }
110
111 /**
112 * Returns a string containing the time in a LocalTime.
113 * The format of the returned string is defined by the pattern.
114 * Attention: any date related characters will be set to 0.
115 * (e.g. 'MM' in the pattern will result in a '00').
116 *
117 * @param time is the LocalTime to process
118 * @param pattern is the pattern to apply
119 * @return a string with the time in the format specified by pattern
120 */
121 public static String getLocalTimeString(LocalTime time, String pattern) {
122 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
123 return fmt.print(time);
124 }
125
126 /**
127 * Returns a DateTime containing the date/time in the string.
128 * The dateTime string must match the pattern so that all values can be set.
129 *
130 * @param dateTime is the string to process
131 * @param pattern is the pattern to apply
132 * @return a DateTime object that contains all values that could be matched
133 */
134 public static DateTime parseDateTime(String dateTime, String pattern) {
135 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
136 return fmt.parseDateTime(dateTime);
137 }
138
139 /**
140 * Returns a LocalDate containing the date in the string.
141 * The date string must match the pattern so that all values can be set.
142 *
143 * @param date is the string to process
144 * @param pattern is the pattern to apply
145 * @return a LocalDate object that contains all values that could be matched
146 */
147 public static LocalDate parseLocalDate(String date, String pattern) {
148 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
149 return fmt.parseDateTime(date).toLocalDate();
150
151 }
152
153 /**
154 * Returns a LocalTime containing the time in the string.
155 * The date string must match the pattern so that all values can be set.
156 *
157 * @param time is the string to process
158 * @param pattern is the pattern to apply
159 * @return a LocalTime object that contains all values that could be matched
160 */
161 public static LocalTime parseLocalTime(String time, String pattern) {
162 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
163 return fmt.parseDateTime(time).toLocalTime();
164 }
165
166 /**
167 * Returns a localized DateTime containing the time in the string.
168 * The dateTime string must match the pattern so that all values can be set.
169 *
170 * @param dateTime is the string to process
171 * @param locale is the local which is applied to the returned DateTime object
172 * @param pattern is the pattern to apply
173 * @return a DateTime object that contains all values that could be matched
174 */
175 public static DateTime parseDateTime(String dateTime, String pattern, Locale locale) {
176 DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern).withLocale(locale);
177 return fmt.parseDateTime(dateTime);
178 }
179
180
181 /**
182 * Creates a DateTime out of a LocalDate and a LocalTime.
183 *
184 * @param date is the LocalDate containing the desired date
185 * @param time is the LocalTime containing the desired time
186 * @return a new DateTime which is created out of the two arguments
187 */
188
189 public static DateTime mergeLocalDateAndLocalTime(LocalDate date, LocalTime time) {
190 return date.toDateTime(time);
191 }
192
193 /**
194 * Changes the time (but not the date) of a DateTime to the time in a LocalTime.
195 *
196 * @param dateTime is the DateTime to set
197 * @param time is the time time to set to
198 * @return a DateTime object with the date of dateTime and the time of time
199 */
200
201 public static DateTime setTimeOfDateTime(DateTime dateTime, LocalTime time) {
202 return dateTime.millisOfDay().setCopy(time.getMillisOfDay());
203 }
204
205 /**
206 * Sets the Date (but not the time) of a DateTime to the value in date.
207 *
208 * @param dateTime is the DateTime for which we set the date
209 * @param date is the LocalDate to set the date to
210 * @return a DateTime object with the date of date and the time of dateTime
211 */
212
213 public static DateTime setDateOfDateTime(DateTime dateTime, LocalDate date) {
214 LocalTime time = dateTime.toLocalTime();
215 DateTime newDateTime = new DateTime(date);
216 newDateTime = newDateTime.millisOfDay().setCopy(time.getMillisOfDay());
217 return newDateTime.withChronology(dateTime.getChronology());
218
219 }
220 }