001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
031     * @author Brian Wing Shun Chan
032     */
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                    // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
045                    // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
046                    // more information.
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                    return equals(date1, date2, false);
079            }
080    
081            public static boolean equals(
082                    Date date1, Date date2, boolean ignoreMilliseconds) {
083    
084                    if (compareTo(date1, date2, ignoreMilliseconds) == 0) {
085                            return true;
086                    }
087    
088                    return false;
089            }
090    
091            public static String getCurrentDate(String pattern, Locale locale) {
092                    return getDate(new Date(), pattern, locale);
093            }
094    
095            public static String getCurrentDate(
096                    String pattern, Locale locale, TimeZone timeZone) {
097    
098                    return getDate(new Date(), pattern, locale, timeZone);
099            }
100    
101            public static String getDate(Date date, String pattern, Locale locale) {
102                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
103                            pattern, locale);
104    
105                    return dateFormat.format(date);
106            }
107    
108            public static String getDate(
109                    Date date, String pattern, Locale locale, TimeZone timeZone) {
110    
111                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
112                            pattern, locale, timeZone);
113    
114                    return dateFormat.format(date);
115            }
116    
117            public static int getDaysBetween(Date date1, Date date2) {
118                    return getDaysBetween(date1, date2, null);
119            }
120    
121            public static int getDaysBetween(
122                    Date date1, Date date2, TimeZone timeZone) {
123    
124                    if (date1.after(date2)) {
125                            Date tempDate = date1;
126    
127                            date1 = date2;
128                            date2 = tempDate;
129                    }
130    
131                    Calendar startCal = null;
132                    Calendar endCal = null;
133    
134                    int offset = 0;
135    
136                    if (timeZone == null) {
137                            startCal = new GregorianCalendar();
138                            endCal = new GregorianCalendar();
139                    }
140                    else {
141                            startCal = new GregorianCalendar(timeZone);
142                            endCal = new GregorianCalendar(timeZone);
143    
144                            offset = timeZone.getRawOffset();
145                    }
146    
147                    startCal.setTime(date1);
148    
149                    startCal.add(Calendar.MILLISECOND, offset);
150    
151                    endCal.setTime(date2);
152    
153                    endCal.add(Calendar.MILLISECOND, offset);
154    
155                    int daysBetween = 0;
156    
157                    while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
158                            startCal.add(Calendar.DAY_OF_MONTH, 1);
159    
160                            daysBetween++;
161                    }
162    
163                    return daysBetween;
164            }
165    
166            public static DateFormat getISO8601Format() {
167                    return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
168            }
169    
170            public static DateFormat getISOFormat() {
171                    return getISOFormat(StringPool.BLANK);
172            }
173    
174            public static DateFormat getISOFormat(String text) {
175                    String pattern = StringPool.BLANK;
176    
177                    if (text.length() == 8) {
178                            pattern = "yyyyMMdd";
179                    }
180                    else if (text.length() == 12) {
181                            pattern = "yyyyMMddHHmm";
182                    }
183                    else if (text.length() == 13) {
184                            pattern = "yyyyMMdd'T'HHmm";
185                    }
186                    else if (text.length() == 14) {
187                            pattern = "yyyyMMddHHmmss";
188                    }
189                    else if (text.length() == 15) {
190                            pattern = "yyyyMMdd'T'HHmmss";
191                    }
192                    else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
193                            pattern = "yyyyMMdd'T'HHmmssz";
194                    }
195                    else {
196                            pattern = "yyyyMMddHHmmssz";
197                    }
198    
199                    return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
200            }
201    
202            public static DateFormat getUTCFormat() {
203                    return getUTCFormat(StringPool.BLANK);
204            }
205    
206            public static DateFormat getUTCFormat(String text) {
207                    String pattern = StringPool.BLANK;
208    
209                    if (text.length() == 8) {
210                            pattern = "yyyyMMdd";
211                    }
212                    else if (text.length() == 12) {
213                            pattern = "yyyyMMddHHmm";
214                    }
215                    else if (text.length() == 13) {
216                            pattern = "yyyyMMdd'T'HHmm";
217                    }
218                    else if (text.length() == 14) {
219                            pattern = "yyyyMMddHHmmss";
220                    }
221                    else if (text.length() == 15) {
222                            pattern = "yyyyMMdd'T'HHmmss";
223                    }
224                    else {
225                            pattern = "yyyyMMdd'T'HHmmssz";
226                    }
227    
228                    return DateFormatFactoryUtil.getSimpleDateFormat(
229                            pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
230            }
231    
232            public static boolean isFormatAmPm(Locale locale) {
233                    Boolean formatAmPm = _formatAmPmMap.get(locale);
234    
235                    if (formatAmPm == null) {
236                            SimpleDateFormat simpleDateFormat =
237                                    (SimpleDateFormat)DateFormat.getTimeInstance(
238                                            DateFormat.SHORT, locale);
239    
240                            String pattern = simpleDateFormat.toPattern();
241    
242                            formatAmPm = pattern.contains("a");
243    
244                            _formatAmPmMap.put(locale, formatAmPm);
245                    }
246    
247                    return formatAmPm;
248            }
249    
250            public static Date newDate() {
251                    return new Date();
252            }
253    
254            public static Date newDate(long date) {
255                    return new Date(date);
256            }
257    
258            public static long newTime() {
259                    Date date = new Date();
260    
261                    return date.getTime();
262            }
263    
264            public static Date parseDate(String dateString, Locale locale)
265                    throws ParseException {
266    
267                    DateFormat dateFormat = DateFormat.getDateInstance(
268                            DateFormat.SHORT, locale);
269    
270                    return dateFormat.parse(dateString);
271            }
272    
273            private static Map<Locale, Boolean> _formatAmPmMap =
274                    new HashMap<Locale, Boolean>();
275    
276    }