1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  import java.util.Locale;
29  import java.util.TimeZone;
30  
31  /**
32   * <a href="DateUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class DateUtil {
38  
39      public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
40  
41      public static int compareTo(Date date1, Date date2) {
42  
43          // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
44          // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
45          // more information.
46  
47          if ((date1 != null) && (date2 == null)) {
48              return -1;
49          }
50          else if ((date1 == null) && (date2 != null)) {
51              return 1;
52          }
53          else if ((date1 == null) && (date2 == null)) {
54              return 0;
55          }
56  
57          long time1 = date1.getTime();
58          long time2 = date2.getTime();
59  
60          if (time1 == time2) {
61              return 0;
62          }
63          else if (time1 < time2) {
64              return -1;
65          }
66          else {
67              return 1;
68          }
69      }
70  
71      public static String getCurrentDate(String pattern, Locale locale) {
72          return getDate(new Date(), pattern, locale);
73      }
74  
75      public static String getCurrentDate(
76          String pattern, Locale locale, TimeZone timeZone) {
77  
78          return getDate(new Date(), pattern, locale, timeZone);
79      }
80  
81      public static String getDate(Date date, String pattern, Locale locale) {
82          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
83  
84          return dateFormat.format(date);
85      }
86  
87      public static String getDate(
88          Date date, String pattern, Locale locale, TimeZone timeZone) {
89  
90          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
91  
92          dateFormat.setTimeZone(timeZone);
93  
94          return dateFormat.format(date);
95      }
96  
97      public static int getDaysBetween(
98          Date startDate, Date endDate, TimeZone timeZone) {
99  
100         int offset = timeZone.getRawOffset();
101 
102         Calendar startCal = new GregorianCalendar(timeZone);
103 
104         startCal.setTime(startDate);
105         startCal.add(Calendar.MILLISECOND, offset);
106 
107         Calendar endCal = new GregorianCalendar(timeZone);
108 
109         endCal.setTime(endDate);
110         endCal.add(Calendar.MILLISECOND, offset);
111 
112         int daysBetween = 0;
113 
114         while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
115             startCal.add(Calendar.DAY_OF_MONTH, 1);
116 
117             daysBetween++;
118         }
119 
120         return daysBetween;
121     }
122 
123     public static DateFormat getISOFormat() {
124         return getISOFormat(StringPool.BLANK);
125     }
126 
127     public static DateFormat getISOFormat(String text) {
128         String pattern = StringPool.BLANK;
129 
130         if (text.length() == 8) {
131             pattern = "yyyyMMdd";
132         }
133         else if (text.length() == 12) {
134             pattern = "yyyyMMddHHmm";
135         }
136         else if (text.length() == 13) {
137             pattern = "yyyyMMdd'T'HHmm";
138         }
139         else if (text.length() == 14) {
140             pattern = "yyyyMMddHHmmss";
141         }
142         else if (text.length() == 15) {
143             pattern = "yyyyMMdd'T'HHmmss";
144         }
145         else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
146             pattern = "yyyyMMdd'T'HHmmssz";
147         }
148         else {
149             pattern = "yyyyMMddHHmmssz";
150         }
151 
152         return new SimpleDateFormat(pattern);
153     }
154 
155     public static DateFormat getISO8601Format() {
156         return new SimpleDateFormat(ISO_8601_PATTERN);
157     }
158 
159     public static DateFormat getUTCFormat() {
160         return getUTCFormat(StringPool.BLANK);
161     }
162 
163     public static DateFormat getUTCFormat(String text) {
164         String pattern = StringPool.BLANK;
165 
166         if (text.length() == 8) {
167             pattern = "yyyyMMdd";
168         }
169         else if (text.length() == 12) {
170             pattern = "yyyyMMddHHmm";
171         }
172         else if (text.length() == 13) {
173             pattern = "yyyyMMdd'T'HHmm";
174         }
175         else if (text.length() == 14) {
176             pattern = "yyyyMMddHHmmss";
177         }
178         else if (text.length() == 15) {
179             pattern = "yyyyMMdd'T'HHmmss";
180         }
181         else {
182             pattern = "yyyyMMdd'T'HHmmssz";
183         }
184 
185         DateFormat dateFormat = new SimpleDateFormat(pattern);
186 
187         dateFormat.setTimeZone(TimeZone.getTimeZone(StringPool.UTC));
188 
189         return dateFormat;
190     }
191 
192     public static Date newDate() {
193         return new Date();
194     }
195 
196     public static Date newDate(long date) {
197         return new Date(date);
198     }
199 
200 }