1   /**
2    * Copyright (c) 2000-2007 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.Date;
29  import java.util.Locale;
30  import java.util.TimeZone;
31  
32  /**
33   * <a href="DateUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class DateUtil {
39  
40      public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
41  
42      public static int compareTo(Date date1, Date date2) {
43  
44          // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
45          // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
46          // more information.
47  
48          if ((date1 != null) && (date2 == null)) {
49              return -1;
50          }
51          else if ((date1 == null) && (date2 != null)) {
52              return 1;
53          }
54          else if ((date1 == null) && (date2 == null)) {
55              return 0;
56          }
57  
58          long time1 = date1.getTime();
59          long time2 = date2.getTime();
60  
61          if (time1 == time2) {
62              return 0;
63          }
64          else if (time1 < time2) {
65              return -1;
66          }
67          else {
68              return 1;
69          }
70      }
71  
72      public static String getCurrentDate(String pattern, Locale locale) {
73          return getDate(new Date(), pattern, locale);
74      }
75  
76      public static String getDate(Date date, String pattern, Locale locale) {
77          DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
78  
79          return dateFormat.format(date);
80      }
81  
82      public static DateFormat getISOFormat() {
83          return getISOFormat(StringPool.BLANK);
84      }
85  
86      public static DateFormat getISOFormat(String text) {
87          String pattern = StringPool.BLANK;
88  
89          if (text.length() == 8) {
90              pattern = "yyyyMMdd";
91          }
92          else if (text.length() == 12) {
93              pattern = "yyyyMMddHHmm";
94          }
95          else if (text.length() == 13) {
96              pattern = "yyyyMMdd'T'HHmm";
97          }
98          else if (text.length() == 14) {
99              pattern = "yyyyMMddHHmmss";
100         }
101         else if (text.length() == 15) {
102             pattern = "yyyyMMdd'T'HHmmss";
103         }
104         else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
105             pattern = "yyyyMMdd'T'HHmmssz";
106         }
107         else {
108             pattern = "yyyyMMddHHmmssz";
109         }
110 
111         return new SimpleDateFormat(pattern);
112     }
113 
114     public static DateFormat getISO8601Format() {
115         return new SimpleDateFormat(ISO_8601_PATTERN);
116     }
117 
118     public static DateFormat getUTCFormat() {
119         return getUTCFormat(StringPool.BLANK);
120     }
121 
122     public static DateFormat getUTCFormat(String text) {
123         String pattern = StringPool.BLANK;
124 
125         if (text.length() == 8) {
126             pattern = "yyyyMMdd";
127         }
128         else if (text.length() == 12) {
129             pattern = "yyyyMMddHHmm";
130         }
131         else if (text.length() == 13) {
132             pattern = "yyyyMMdd'T'HHmm";
133         }
134         else if (text.length() == 14) {
135             pattern = "yyyyMMddHHmmss";
136         }
137         else if (text.length() == 15) {
138             pattern = "yyyyMMdd'T'HHmmss";
139         }
140         else {
141             pattern = "yyyyMMdd'T'HHmmssz";
142         }
143 
144         DateFormat dateFormat = new SimpleDateFormat(pattern);
145 
146         dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
147 
148         return dateFormat;
149     }
150 
151 }