1   /**
2    * Copyright (c) 2000-2008 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 getDate(Date date, String pattern, Locale locale) {
79          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
80  
81          return dateFormat.format(date);
82      }
83  
84      public static int getDaysBetween(
85          Date startDate, Date endDate, TimeZone timeZone) {
86  
87          Calendar startCal = new GregorianCalendar(timeZone);
88  
89          startCal.setTime(startDate);
90  
91          Calendar endCal = new GregorianCalendar(timeZone);
92  
93          endCal.setTime(endDate);
94  
95          int daysBetween = 0;
96  
97          while (startCal.before(endCal)) {
98              startCal.add(Calendar.DAY_OF_MONTH, 1);
99  
100             if (startCal.before(endCal)) {
101                 daysBetween++;
102             }
103         }
104 
105         return daysBetween;
106     }
107 
108     public static DateFormat getISOFormat() {
109         return getISOFormat(StringPool.BLANK);
110     }
111 
112     public static DateFormat getISOFormat(String text) {
113         String pattern = StringPool.BLANK;
114 
115         if (text.length() == 8) {
116             pattern = "yyyyMMdd";
117         }
118         else if (text.length() == 12) {
119             pattern = "yyyyMMddHHmm";
120         }
121         else if (text.length() == 13) {
122             pattern = "yyyyMMdd'T'HHmm";
123         }
124         else if (text.length() == 14) {
125             pattern = "yyyyMMddHHmmss";
126         }
127         else if (text.length() == 15) {
128             pattern = "yyyyMMdd'T'HHmmss";
129         }
130         else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
131             pattern = "yyyyMMdd'T'HHmmssz";
132         }
133         else {
134             pattern = "yyyyMMddHHmmssz";
135         }
136 
137         return new SimpleDateFormat(pattern);
138     }
139 
140     public static DateFormat getISO8601Format() {
141         return new SimpleDateFormat(ISO_8601_PATTERN);
142     }
143 
144     public static DateFormat getUTCFormat() {
145         return getUTCFormat(StringPool.BLANK);
146     }
147 
148     public static DateFormat getUTCFormat(String text) {
149         String pattern = StringPool.BLANK;
150 
151         if (text.length() == 8) {
152             pattern = "yyyyMMdd";
153         }
154         else if (text.length() == 12) {
155             pattern = "yyyyMMddHHmm";
156         }
157         else if (text.length() == 13) {
158             pattern = "yyyyMMdd'T'HHmm";
159         }
160         else if (text.length() == 14) {
161             pattern = "yyyyMMddHHmmss";
162         }
163         else if (text.length() == 15) {
164             pattern = "yyyyMMdd'T'HHmmss";
165         }
166         else {
167             pattern = "yyyyMMdd'T'HHmmssz";
168         }
169 
170         DateFormat dateFormat = new SimpleDateFormat(pattern);
171 
172         dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
173 
174         return dateFormat;
175     }
176 
177 }