1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.text.DateFormat;
26  import java.text.Format;
27  
28  import java.util.Calendar;
29  import java.util.Date;
30  import java.util.GregorianCalendar;
31  import java.util.Locale;
32  import java.util.TimeZone;
33  
34  /**
35   * <a href="DateUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class DateUtil {
40  
41      public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
42  
43      public static int compareTo(Date date1, Date date2) {
44          return compareTo(date1, date2, false);
45      }
46  
47      public static int compareTo(
48          Date date1, Date date2, boolean ignoreMilliseconds) {
49  
50          // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
51          // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
52          // more information.
53  
54          if ((date1 != null) && (date2 == null)) {
55              return -1;
56          }
57          else if ((date1 == null) && (date2 != null)) {
58              return 1;
59          }
60          else if ((date1 == null) && (date2 == null)) {
61              return 0;
62          }
63  
64          long time1 = date1.getTime();
65          long time2 = date2.getTime();
66  
67          if (ignoreMilliseconds) {
68              time1 = time1 / Time.SECOND;
69              time2 = time2 / Time.SECOND;
70          }
71  
72          if (time1 == time2) {
73              return 0;
74          }
75          else if (time1 < time2) {
76              return -1;
77          }
78          else {
79              return 1;
80          }
81      }
82  
83      public static boolean equals(Date date1, Date date2) {
84          if (compareTo(date1, date2) == 0) {
85              return true;
86          }
87          else {
88              return false;
89          }
90      }
91  
92      public static boolean equals(
93          Date date1, Date date2, boolean ignoreMilliseconds) {
94  
95          if (!ignoreMilliseconds) {
96              return equals(date1, date2);
97          }
98  
99          long time1 = 0;
100 
101         if (date1 != null) {
102             time1 = date1.getTime() / Time.SECOND;
103         }
104 
105         long time2 = 0;
106 
107         if (date2 != null) {
108             time2 = date2.getTime() / Time.SECOND;
109         }
110 
111         if (time1 == time2) {
112             return true;
113         }
114         else {
115             return false;
116         }
117     }
118 
119     public static String getCurrentDate(String pattern, Locale locale) {
120         return getDate(new Date(), pattern, locale);
121     }
122 
123     public static String getCurrentDate(
124         String pattern, Locale locale, TimeZone timeZone) {
125 
126         return getDate(new Date(), pattern, locale, timeZone);
127     }
128 
129     public static String getDate(Date date, String pattern, Locale locale) {
130         Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
131             pattern, locale);
132 
133         return dateFormat.format(date);
134     }
135 
136     public static String getDate(
137         Date date, String pattern, Locale locale, TimeZone timeZone) {
138 
139         Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
140             pattern, locale, timeZone);
141 
142         return dateFormat.format(date);
143     }
144 
145     public static int getDaysBetween(
146         Date startDate, Date endDate, TimeZone timeZone) {
147 
148         int offset = timeZone.getRawOffset();
149 
150         Calendar startCal = new GregorianCalendar(timeZone);
151 
152         startCal.setTime(startDate);
153         startCal.add(Calendar.MILLISECOND, offset);
154 
155         Calendar endCal = new GregorianCalendar(timeZone);
156 
157         endCal.setTime(endDate);
158         endCal.add(Calendar.MILLISECOND, offset);
159 
160         int daysBetween = 0;
161 
162         while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
163             startCal.add(Calendar.DAY_OF_MONTH, 1);
164 
165             daysBetween++;
166         }
167 
168         return daysBetween;
169     }
170 
171     public static DateFormat getISOFormat() {
172         return getISOFormat(StringPool.BLANK);
173     }
174 
175     public static DateFormat getISOFormat(String text) {
176         String pattern = StringPool.BLANK;
177 
178         if (text.length() == 8) {
179             pattern = "yyyyMMdd";
180         }
181         else if (text.length() == 12) {
182             pattern = "yyyyMMddHHmm";
183         }
184         else if (text.length() == 13) {
185             pattern = "yyyyMMdd'T'HHmm";
186         }
187         else if (text.length() == 14) {
188             pattern = "yyyyMMddHHmmss";
189         }
190         else if (text.length() == 15) {
191             pattern = "yyyyMMdd'T'HHmmss";
192         }
193         else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
194             pattern = "yyyyMMdd'T'HHmmssz";
195         }
196         else {
197             pattern = "yyyyMMddHHmmssz";
198         }
199 
200         return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
201     }
202 
203     public static DateFormat getISO8601Format() {
204         return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
205     }
206 
207     public static DateFormat getUTCFormat() {
208         return getUTCFormat(StringPool.BLANK);
209     }
210 
211     public static DateFormat getUTCFormat(String text) {
212         String pattern = StringPool.BLANK;
213 
214         if (text.length() == 8) {
215             pattern = "yyyyMMdd";
216         }
217         else if (text.length() == 12) {
218             pattern = "yyyyMMddHHmm";
219         }
220         else if (text.length() == 13) {
221             pattern = "yyyyMMdd'T'HHmm";
222         }
223         else if (text.length() == 14) {
224             pattern = "yyyyMMddHHmmss";
225         }
226         else if (text.length() == 15) {
227             pattern = "yyyyMMdd'T'HHmmss";
228         }
229         else {
230             pattern = "yyyyMMdd'T'HHmmssz";
231         }
232 
233         return DateFormatFactoryUtil.getSimpleDateFormat(
234             pattern, TimeZone.getTimeZone(StringPool.UTC));
235     }
236 
237     public static Date newDate() {
238         return new Date();
239     }
240 
241     public static Date newDate(long date) {
242         return new Date(date);
243     }
244 
245 }