001    /**
002     * Copyright (c) 2000-2012 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 deltaTime = date1.getTime() - date2.getTime();
090    
091                    if ((deltaTime > -1000) && (deltaTime < 1000)) {
092                            return true;
093                    }
094                    else {
095                            return false;
096                    }
097            }
098    
099            public static String getCurrentDate(String pattern, Locale locale) {
100                    return getDate(new Date(), pattern, locale);
101            }
102    
103            public static String getCurrentDate(
104                    String pattern, Locale locale, TimeZone timeZone) {
105    
106                    return getDate(new Date(), pattern, locale, timeZone);
107            }
108    
109            public static String getDate(Date date, String pattern, Locale locale) {
110                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
111                            pattern, locale);
112    
113                    return dateFormat.format(date);
114            }
115    
116            public static String getDate(
117                    Date date, String pattern, Locale locale, TimeZone timeZone) {
118    
119                    Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
120                            pattern, locale, timeZone);
121    
122                    return dateFormat.format(date);
123            }
124    
125            public static int getDaysBetween(
126                    Date startDate, Date endDate, TimeZone timeZone) {
127    
128                    int offset = timeZone.getRawOffset();
129    
130                    Calendar startCal = new GregorianCalendar(timeZone);
131    
132                    startCal.setTime(startDate);
133                    startCal.add(Calendar.MILLISECOND, offset);
134    
135                    Calendar endCal = new GregorianCalendar(timeZone);
136    
137                    endCal.setTime(endDate);
138                    endCal.add(Calendar.MILLISECOND, offset);
139    
140                    int daysBetween = 0;
141    
142                    while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
143                            startCal.add(Calendar.DAY_OF_MONTH, 1);
144    
145                            daysBetween++;
146                    }
147    
148                    return daysBetween;
149            }
150    
151            public static DateFormat getISO8601Format() {
152                    return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
153            }
154    
155            public static DateFormat getISOFormat() {
156                    return getISOFormat(StringPool.BLANK);
157            }
158    
159            public static DateFormat getISOFormat(String text) {
160                    String pattern = StringPool.BLANK;
161    
162                    if (text.length() == 8) {
163                            pattern = "yyyyMMdd";
164                    }
165                    else if (text.length() == 12) {
166                            pattern = "yyyyMMddHHmm";
167                    }
168                    else if (text.length() == 13) {
169                            pattern = "yyyyMMdd'T'HHmm";
170                    }
171                    else if (text.length() == 14) {
172                            pattern = "yyyyMMddHHmmss";
173                    }
174                    else if (text.length() == 15) {
175                            pattern = "yyyyMMdd'T'HHmmss";
176                    }
177                    else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
178                            pattern = "yyyyMMdd'T'HHmmssz";
179                    }
180                    else {
181                            pattern = "yyyyMMddHHmmssz";
182                    }
183    
184                    return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
185            }
186    
187            public static DateFormat getUTCFormat() {
188                    return getUTCFormat(StringPool.BLANK);
189            }
190    
191            public static DateFormat getUTCFormat(String text) {
192                    String pattern = StringPool.BLANK;
193    
194                    if (text.length() == 8) {
195                            pattern = "yyyyMMdd";
196                    }
197                    else if (text.length() == 12) {
198                            pattern = "yyyyMMddHHmm";
199                    }
200                    else if (text.length() == 13) {
201                            pattern = "yyyyMMdd'T'HHmm";
202                    }
203                    else if (text.length() == 14) {
204                            pattern = "yyyyMMddHHmmss";
205                    }
206                    else if (text.length() == 15) {
207                            pattern = "yyyyMMdd'T'HHmmss";
208                    }
209                    else {
210                            pattern = "yyyyMMdd'T'HHmmssz";
211                    }
212    
213                    return DateFormatFactoryUtil.getSimpleDateFormat(
214                            pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
215            }
216    
217            public static Date newDate() {
218                    return new Date();
219            }
220    
221            public static Date newDate(long date) {
222                    return new Date(date);
223            }
224    
225            public static long newTime() {
226                    Date date = new Date();
227    
228                    return date.getTime();
229            }
230    
231    }