001    /**
002     * Copyright (c) 2000-2010 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    
020    import java.util.Calendar;
021    import java.util.Date;
022    import java.util.GregorianCalendar;
023    import java.util.Locale;
024    import java.util.TimeZone;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class DateUtil {
030    
031            public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
032    
033            public static int compareTo(Date date1, Date date2) {
034                    return compareTo(date1, date2, false);
035            }
036    
037            public static int compareTo(
038                    Date date1, Date date2, boolean ignoreMilliseconds) {
039    
040                    // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
041                    // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
042                    // more information.
043    
044                    if ((date1 != null) && (date2 == null)) {
045                            return -1;
046                    }
047                    else if ((date1 == null) && (date2 != null)) {
048                            return 1;
049                    }
050                    else if ((date1 == null) && (date2 == null)) {
051                            return 0;
052                    }
053    
054                    long time1 = date1.getTime();
055                    long time2 = date2.getTime();
056    
057                    if (ignoreMilliseconds) {
058                            time1 = time1 / Time.SECOND;
059                            time2 = time2 / Time.SECOND;
060                    }
061    
062                    if (time1 == time2) {
063                            return 0;
064                    }
065                    else if (time1 < time2) {
066                            return -1;
067                    }
068                    else {
069                            return 1;
070                    }
071            }
072    
073            public static boolean equals(Date date1, Date date2) {
074                    if (compareTo(date1, date2) == 0) {
075                            return true;
076                    }
077                    else {
078                            return false;
079                    }
080            }
081    
082            public static boolean equals(
083                    Date date1, Date date2, boolean ignoreMilliseconds) {
084    
085                    if (!ignoreMilliseconds) {
086                            return equals(date1, date2);
087                    }
088    
089                    long time1 = 0;
090    
091                    if (date1 != null) {
092                            time1 = date1.getTime() / Time.SECOND;
093                    }
094    
095                    long time2 = 0;
096    
097                    if (date2 != null) {
098                            time2 = date2.getTime() / Time.SECOND;
099                    }
100    
101                    if (time1 == time2) {
102                            return true;
103                    }
104                    else {
105                            return false;
106                    }
107            }
108    
109            public static String getCurrentDate(String pattern, Locale locale) {
110                    return getDate(new Date(), pattern, locale);
111            }
112    
113            public static String getCurrentDate(
114                    String pattern, Locale locale, TimeZone timeZone) {
115    
116                    return getDate(new Date(), pattern, locale, timeZone);
117            }
118    
119            public static String getDate(Date date, String pattern, Locale locale) {
120                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
121                            pattern, locale);
122    
123                    return dateFormat.format(date);
124            }
125    
126            public static String getDate(
127                    Date date, String pattern, Locale locale, TimeZone timeZone) {
128    
129                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
130                            pattern, locale, timeZone);
131    
132                    return dateFormat.format(date);
133            }
134    
135            public static int getDaysBetween(
136                    Date startDate, Date endDate, TimeZone timeZone) {
137    
138                    int offset = timeZone.getRawOffset();
139    
140                    Calendar startCal = new GregorianCalendar(timeZone);
141    
142                    startCal.setTime(startDate);
143                    startCal.add(Calendar.MILLISECOND, offset);
144    
145                    Calendar endCal = new GregorianCalendar(timeZone);
146    
147                    endCal.setTime(endDate);
148                    endCal.add(Calendar.MILLISECOND, offset);
149    
150                    int daysBetween = 0;
151    
152                    while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
153                            startCal.add(Calendar.DAY_OF_MONTH, 1);
154    
155                            daysBetween++;
156                    }
157    
158                    return daysBetween;
159            }
160    
161            public static DateFormat getISOFormat() {
162                    return getISOFormat(StringPool.BLANK);
163            }
164    
165            public static DateFormat getISOFormat(String text) {
166                    String pattern = StringPool.BLANK;
167    
168                    if (text.length() == 8) {
169                            pattern = "yyyyMMdd";
170                    }
171                    else if (text.length() == 12) {
172                            pattern = "yyyyMMddHHmm";
173                    }
174                    else if (text.length() == 13) {
175                            pattern = "yyyyMMdd'T'HHmm";
176                    }
177                    else if (text.length() == 14) {
178                            pattern = "yyyyMMddHHmmss";
179                    }
180                    else if (text.length() == 15) {
181                            pattern = "yyyyMMdd'T'HHmmss";
182                    }
183                    else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
184                            pattern = "yyyyMMdd'T'HHmmssz";
185                    }
186                    else {
187                            pattern = "yyyyMMddHHmmssz";
188                    }
189    
190                    return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
191            }
192    
193            public static DateFormat getISO8601Format() {
194                    return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
195            }
196    
197            public static DateFormat getUTCFormat() {
198                    return getUTCFormat(StringPool.BLANK);
199            }
200    
201            public static DateFormat getUTCFormat(String text) {
202                    String pattern = StringPool.BLANK;
203    
204                    if (text.length() == 8) {
205                            pattern = "yyyyMMdd";
206                    }
207                    else if (text.length() == 12) {
208                            pattern = "yyyyMMddHHmm";
209                    }
210                    else if (text.length() == 13) {
211                            pattern = "yyyyMMdd'T'HHmm";
212                    }
213                    else if (text.length() == 14) {
214                            pattern = "yyyyMMddHHmmss";
215                    }
216                    else if (text.length() == 15) {
217                            pattern = "yyyyMMdd'T'HHmmss";
218                    }
219                    else {
220                            pattern = "yyyyMMdd'T'HHmmssz";
221                    }
222    
223                    return DateFormatFactoryUtil.getSimpleDateFormat(
224                            pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
225            }
226    
227            public static Date newDate() {
228                    return new Date();
229            }
230    
231            public static Date newDate(long date) {
232                    return new Date(date);
233            }
234    
235    }