001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
021    import java.util.Calendar;
022    import java.util.Date;
023    import java.util.GregorianCalendar;
024    import java.util.Locale;
025    import java.util.TimeZone;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class DateUtil {
031    
032            public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
033    
034            public static int compareTo(Date date1, Date date2) {
035                    return compareTo(date1, date2, false);
036            }
037    
038            public static int compareTo(
039                    Date date1, Date date2, boolean ignoreMilliseconds) {
040    
041                    // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
042                    // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
043                    // more information.
044    
045                    if ((date1 != null) && (date2 == null)) {
046                            return -1;
047                    }
048                    else if ((date1 == null) && (date2 != null)) {
049                            return 1;
050                    }
051                    else if ((date1 == null) && (date2 == null)) {
052                            return 0;
053                    }
054    
055                    long time1 = date1.getTime();
056                    long time2 = date2.getTime();
057    
058                    if (ignoreMilliseconds) {
059                            time1 = time1 / Time.SECOND;
060                            time2 = time2 / Time.SECOND;
061                    }
062    
063                    if (time1 == time2) {
064                            return 0;
065                    }
066                    else if (time1 < time2) {
067                            return -1;
068                    }
069                    else {
070                            return 1;
071                    }
072            }
073    
074            public static boolean equals(Date date1, Date date2) {
075                    if (compareTo(date1, date2) == 0) {
076                            return true;
077                    }
078                    else {
079                            return false;
080                    }
081            }
082    
083            public static boolean equals(
084                    Date date1, Date date2, boolean ignoreMilliseconds) {
085    
086                    if (!ignoreMilliseconds) {
087                            return equals(date1, date2);
088                    }
089    
090                    long deltaTime = date1.getTime() - date2.getTime();
091    
092                    if ((deltaTime > -1000) && (deltaTime < 1000)) {
093                            return true;
094                    }
095                    else {
096                            return false;
097                    }
098            }
099    
100            public static String getCurrentDate(String pattern, Locale locale) {
101                    return getDate(new Date(), pattern, locale);
102            }
103    
104            public static String getCurrentDate(
105                    String pattern, Locale locale, TimeZone timeZone) {
106    
107                    return getDate(new Date(), pattern, locale, timeZone);
108            }
109    
110            public static String getDate(Date date, String pattern, Locale locale) {
111                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
112                            pattern, locale);
113    
114                    return dateFormat.format(date);
115            }
116    
117            public static String getDate(
118                    Date date, String pattern, Locale locale, TimeZone timeZone) {
119    
120                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
121                            pattern, locale, timeZone);
122    
123                    return dateFormat.format(date);
124            }
125    
126            public static int getDaysBetween(Date date1, Date date2) {
127                    return getDaysBetween(date1, date2, null);
128            }
129    
130            public static int getDaysBetween(
131                    Date date1, Date date2, TimeZone timeZone) {
132    
133                    if (date1.after(date2)) {
134                            Date tempDate = date1;
135    
136                            date1 = date2;
137                            date2 = tempDate;
138                    }
139    
140                    Calendar startCal = null;
141                    Calendar endCal = null;
142    
143                    int offset = 0;
144    
145                    if (timeZone == null) {
146                            startCal = new GregorianCalendar();
147                            endCal = new GregorianCalendar();
148                    }
149                    else {
150                            startCal = new GregorianCalendar(timeZone);
151                            endCal = new GregorianCalendar(timeZone);
152    
153                            offset = timeZone.getRawOffset();
154                    }
155    
156                    startCal.setTime(date1);
157    
158                    startCal.add(Calendar.MILLISECOND, offset);
159    
160                    endCal.setTime(date2);
161    
162                    endCal.add(Calendar.MILLISECOND, offset);
163    
164                    int daysBetween = 0;
165    
166                    while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
167                            startCal.add(Calendar.DAY_OF_MONTH, 1);
168    
169                            daysBetween++;
170                    }
171    
172                    return daysBetween;
173            }
174    
175            public static DateFormat getISO8601Format() {
176                    return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
177            }
178    
179            public static DateFormat getISOFormat() {
180                    return getISOFormat(StringPool.BLANK);
181            }
182    
183            public static DateFormat getISOFormat(String text) {
184                    String pattern = StringPool.BLANK;
185    
186                    if (text.length() == 8) {
187                            pattern = "yyyyMMdd";
188                    }
189                    else if (text.length() == 12) {
190                            pattern = "yyyyMMddHHmm";
191                    }
192                    else if (text.length() == 13) {
193                            pattern = "yyyyMMdd'T'HHmm";
194                    }
195                    else if (text.length() == 14) {
196                            pattern = "yyyyMMddHHmmss";
197                    }
198                    else if (text.length() == 15) {
199                            pattern = "yyyyMMdd'T'HHmmss";
200                    }
201                    else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
202                            pattern = "yyyyMMdd'T'HHmmssz";
203                    }
204                    else {
205                            pattern = "yyyyMMddHHmmssz";
206                    }
207    
208                    return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
209            }
210    
211            public static DateFormat getUTCFormat() {
212                    return getUTCFormat(StringPool.BLANK);
213            }
214    
215            public static DateFormat getUTCFormat(String text) {
216                    String pattern = StringPool.BLANK;
217    
218                    if (text.length() == 8) {
219                            pattern = "yyyyMMdd";
220                    }
221                    else if (text.length() == 12) {
222                            pattern = "yyyyMMddHHmm";
223                    }
224                    else if (text.length() == 13) {
225                            pattern = "yyyyMMdd'T'HHmm";
226                    }
227                    else if (text.length() == 14) {
228                            pattern = "yyyyMMddHHmmss";
229                    }
230                    else if (text.length() == 15) {
231                            pattern = "yyyyMMdd'T'HHmmss";
232                    }
233                    else {
234                            pattern = "yyyyMMdd'T'HHmmssz";
235                    }
236    
237                    return DateFormatFactoryUtil.getSimpleDateFormat(
238                            pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
239            }
240    
241            public static Date newDate() {
242                    return new Date();
243            }
244    
245            public static Date newDate(long date) {
246                    return new Date(date);
247            }
248    
249            public static long newTime() {
250                    Date date = new Date();
251    
252                    return date.getTime();
253            }
254    
255            public static Date parseDate(String dateString, Locale locale)
256                    throws ParseException {
257    
258                    DateFormat dateFormat = DateFormat.getDateInstance(
259                            DateFormat.SHORT, locale);
260    
261                    return dateFormat.parse(dateString);
262            }
263    
264    }