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.SimpleDateFormat;
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   */
40  public class DateUtil {
41  
42      public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
43  
44      public static int compareTo(Date date1, Date date2) {
45  
46          // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
47          // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
48          // more information.
49  
50          if ((date1 != null) && (date2 == null)) {
51              return -1;
52          }
53          else if ((date1 == null) && (date2 != null)) {
54              return 1;
55          }
56          else if ((date1 == null) && (date2 == null)) {
57              return 0;
58          }
59  
60          long time1 = date1.getTime();
61          long time2 = date2.getTime();
62  
63          if (time1 == time2) {
64              return 0;
65          }
66          else if (time1 < time2) {
67              return -1;
68          }
69          else {
70              return 1;
71          }
72      }
73  
74      public static String getCurrentDate(String pattern, Locale locale) {
75          return getDate(new Date(), pattern, locale);
76      }
77  
78      public static String getCurrentDate(
79          String pattern, Locale locale, TimeZone timeZone) {
80  
81          return getDate(new Date(), pattern, locale, timeZone);
82      }
83  
84      public static String getDate(Date date, String pattern, Locale locale) {
85          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
86  
87          return dateFormat.format(date);
88      }
89  
90      public static String getDate(
91          Date date, String pattern, Locale locale, TimeZone timeZone) {
92  
93          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
94  
95          dateFormat.setTimeZone(timeZone);
96  
97          return dateFormat.format(date);
98      }
99  
100     public static int getDaysBetween(
101         Date startDate, Date endDate, TimeZone timeZone) {
102 
103         int offset = timeZone.getRawOffset();
104 
105         Calendar startCal = new GregorianCalendar(timeZone);
106 
107         startCal.setTime(startDate);
108         startCal.add(Calendar.MILLISECOND, offset);
109 
110         Calendar endCal = new GregorianCalendar(timeZone);
111 
112         endCal.setTime(endDate);
113         endCal.add(Calendar.MILLISECOND, offset);
114 
115         int daysBetween = 0;
116 
117         while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
118             startCal.add(Calendar.DAY_OF_MONTH, 1);
119 
120             daysBetween++;
121         }
122 
123         return daysBetween;
124     }
125 
126     public static DateFormat getISOFormat() {
127         return getISOFormat(StringPool.BLANK);
128     }
129 
130     public static DateFormat getISOFormat(String text) {
131         String pattern = StringPool.BLANK;
132 
133         if (text.length() == 8) {
134             pattern = "yyyyMMdd";
135         }
136         else if (text.length() == 12) {
137             pattern = "yyyyMMddHHmm";
138         }
139         else if (text.length() == 13) {
140             pattern = "yyyyMMdd'T'HHmm";
141         }
142         else if (text.length() == 14) {
143             pattern = "yyyyMMddHHmmss";
144         }
145         else if (text.length() == 15) {
146             pattern = "yyyyMMdd'T'HHmmss";
147         }
148         else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
149             pattern = "yyyyMMdd'T'HHmmssz";
150         }
151         else {
152             pattern = "yyyyMMddHHmmssz";
153         }
154 
155         return new SimpleDateFormat(pattern);
156     }
157 
158     public static DateFormat getISO8601Format() {
159         return new SimpleDateFormat(ISO_8601_PATTERN);
160     }
161 
162     public static DateFormat getUTCFormat() {
163         return getUTCFormat(StringPool.BLANK);
164     }
165 
166     public static DateFormat getUTCFormat(String text) {
167         String pattern = StringPool.BLANK;
168 
169         if (text.length() == 8) {
170             pattern = "yyyyMMdd";
171         }
172         else if (text.length() == 12) {
173             pattern = "yyyyMMddHHmm";
174         }
175         else if (text.length() == 13) {
176             pattern = "yyyyMMdd'T'HHmm";
177         }
178         else if (text.length() == 14) {
179             pattern = "yyyyMMddHHmmss";
180         }
181         else if (text.length() == 15) {
182             pattern = "yyyyMMdd'T'HHmmss";
183         }
184         else {
185             pattern = "yyyyMMdd'T'HHmmssz";
186         }
187 
188         DateFormat dateFormat = new SimpleDateFormat(pattern);
189 
190         dateFormat.setTimeZone(TimeZone.getTimeZone(StringPool.UTC));
191 
192         return dateFormat;
193     }
194 
195     public static Date newDate() {
196         return new Date();
197     }
198 
199     public static Date newDate(long date) {
200         return new Date(date);
201     }
202 
203 }