001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.text.DateFormat;
018 import java.text.Format;
019 import java.text.ParseException;
020 import java.text.SimpleDateFormat;
021
022 import java.util.Calendar;
023 import java.util.Date;
024 import java.util.GregorianCalendar;
025 import java.util.HashMap;
026 import java.util.Locale;
027 import java.util.Map;
028 import java.util.TimeZone;
029
030
033 public class DateUtil {
034
035 public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
036
037 public static int compareTo(Date date1, Date date2) {
038 return compareTo(date1, date2, false);
039 }
040
041 public static int compareTo(
042 Date date1, Date date2, boolean ignoreMilliseconds) {
043
044
045
046
047
048 if ((date1 != null) && (date2 == null)) {
049 return -1;
050 }
051 else if ((date1 == null) && (date2 != null)) {
052 return 1;
053 }
054 else if ((date1 == null) && (date2 == null)) {
055 return 0;
056 }
057
058 long time1 = date1.getTime();
059 long time2 = date2.getTime();
060
061 if (ignoreMilliseconds) {
062 time1 = time1 / Time.SECOND;
063 time2 = time2 / Time.SECOND;
064 }
065
066 if (time1 == time2) {
067 return 0;
068 }
069 else if (time1 < time2) {
070 return -1;
071 }
072 else {
073 return 1;
074 }
075 }
076
077 public static boolean equals(Date date1, Date date2) {
078 if (compareTo(date1, date2) == 0) {
079 return true;
080 }
081 else {
082 return false;
083 }
084 }
085
086 public static boolean equals(
087 Date date1, Date date2, boolean ignoreMilliseconds) {
088
089 if (!ignoreMilliseconds) {
090 return equals(date1, date2);
091 }
092
093 long deltaTime = date1.getTime() - date2.getTime();
094
095 if ((deltaTime > -1000) && (deltaTime < 1000)) {
096 return true;
097 }
098 else {
099 return false;
100 }
101 }
102
103 public static String getCurrentDate(String pattern, Locale locale) {
104 return getDate(new Date(), pattern, locale);
105 }
106
107 public static String getCurrentDate(
108 String pattern, Locale locale, TimeZone timeZone) {
109
110 return getDate(new Date(), pattern, locale, timeZone);
111 }
112
113 public static String getDate(Date date, String pattern, Locale locale) {
114 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
115 pattern, locale);
116
117 return dateFormat.format(date);
118 }
119
120 public static String getDate(
121 Date date, String pattern, Locale locale, TimeZone timeZone) {
122
123 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
124 pattern, locale, timeZone);
125
126 return dateFormat.format(date);
127 }
128
129 public static int getDaysBetween(Date date1, Date date2) {
130 return getDaysBetween(date1, date2, null);
131 }
132
133 public static int getDaysBetween(
134 Date date1, Date date2, TimeZone timeZone) {
135
136 if (date1.after(date2)) {
137 Date tempDate = date1;
138
139 date1 = date2;
140 date2 = tempDate;
141 }
142
143 Calendar startCal = null;
144 Calendar endCal = null;
145
146 int offset = 0;
147
148 if (timeZone == null) {
149 startCal = new GregorianCalendar();
150 endCal = new GregorianCalendar();
151 }
152 else {
153 startCal = new GregorianCalendar(timeZone);
154 endCal = new GregorianCalendar(timeZone);
155
156 offset = timeZone.getRawOffset();
157 }
158
159 startCal.setTime(date1);
160
161 startCal.add(Calendar.MILLISECOND, offset);
162
163 endCal.setTime(date2);
164
165 endCal.add(Calendar.MILLISECOND, offset);
166
167 int daysBetween = 0;
168
169 while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
170 startCal.add(Calendar.DAY_OF_MONTH, 1);
171
172 daysBetween++;
173 }
174
175 return daysBetween;
176 }
177
178 public static DateFormat getISO8601Format() {
179 return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
180 }
181
182 public static DateFormat getISOFormat() {
183 return getISOFormat(StringPool.BLANK);
184 }
185
186 public static DateFormat getISOFormat(String text) {
187 String pattern = StringPool.BLANK;
188
189 if (text.length() == 8) {
190 pattern = "yyyyMMdd";
191 }
192 else if (text.length() == 12) {
193 pattern = "yyyyMMddHHmm";
194 }
195 else if (text.length() == 13) {
196 pattern = "yyyyMMdd'T'HHmm";
197 }
198 else if (text.length() == 14) {
199 pattern = "yyyyMMddHHmmss";
200 }
201 else if (text.length() == 15) {
202 pattern = "yyyyMMdd'T'HHmmss";
203 }
204 else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
205 pattern = "yyyyMMdd'T'HHmmssz";
206 }
207 else {
208 pattern = "yyyyMMddHHmmssz";
209 }
210
211 return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
212 }
213
214 public static DateFormat getUTCFormat() {
215 return getUTCFormat(StringPool.BLANK);
216 }
217
218 public static DateFormat getUTCFormat(String text) {
219 String pattern = StringPool.BLANK;
220
221 if (text.length() == 8) {
222 pattern = "yyyyMMdd";
223 }
224 else if (text.length() == 12) {
225 pattern = "yyyyMMddHHmm";
226 }
227 else if (text.length() == 13) {
228 pattern = "yyyyMMdd'T'HHmm";
229 }
230 else if (text.length() == 14) {
231 pattern = "yyyyMMddHHmmss";
232 }
233 else if (text.length() == 15) {
234 pattern = "yyyyMMdd'T'HHmmss";
235 }
236 else {
237 pattern = "yyyyMMdd'T'HHmmssz";
238 }
239
240 return DateFormatFactoryUtil.getSimpleDateFormat(
241 pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
242 }
243
244 public static boolean isFormatAmPm(Locale locale) {
245 Boolean formatAmPm = _formatAmPmMap.get(locale);
246
247 if (formatAmPm == null) {
248 SimpleDateFormat simpleDateFormat =
249 (SimpleDateFormat)DateFormat.getTimeInstance(
250 DateFormat.SHORT, locale);
251
252 String pattern = simpleDateFormat.toPattern();
253
254 formatAmPm = pattern.contains("a");
255
256 _formatAmPmMap.put(locale, formatAmPm);
257 }
258
259 return formatAmPm;
260 }
261
262 public static Date newDate() {
263 return new Date();
264 }
265
266 public static Date newDate(long date) {
267 return new Date(date);
268 }
269
270 public static long newTime() {
271 Date date = new Date();
272
273 return date.getTime();
274 }
275
276 public static Date parseDate(String dateString, Locale locale)
277 throws ParseException {
278
279 DateFormat dateFormat = DateFormat.getDateInstance(
280 DateFormat.SHORT, locale);
281
282 return dateFormat.parse(dateString);
283 }
284
285 private static Map<Locale, Boolean> _formatAmPmMap =
286 new HashMap<Locale, Boolean>();
287
288 }