1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.Format;
26  
27  import java.util.Calendar;
28  import java.util.Date;
29  import java.util.TimeZone;
30  
31  /**
32   * <a href="Time.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class Time {
37  
38      public static final long SECOND = 1000;
39  
40      public static final long MINUTE = SECOND * 60;
41  
42      public static final long HOUR = MINUTE * 60;
43  
44      public static final long DAY = HOUR * 24;
45  
46      public static final long WEEK = DAY * 7;
47  
48      public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
49  
50      public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
51  
52      public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
53  
54      public static Date getDate(Calendar cal) {
55          Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
56  
57          adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
58          adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
59          adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
60          adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
61          adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
62          adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
63          adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
64  
65          return adjustedCal.getTime();
66      }
67  
68      public static Date getDate(TimeZone tz) {
69          Calendar cal = CalendarFactoryUtil.getCalendar(tz);
70  
71          return getDate(cal);
72      }
73  
74      public static Date getDate(Date date, TimeZone tz) {
75          Calendar cal = CalendarFactoryUtil.getCalendar(tz);
76  
77          cal.setTime(date);
78  
79          return getDate(cal);
80      }
81  
82      public static String getDescription(long milliseconds) {
83          String s = "";
84  
85          int x = 0;
86  
87          if (milliseconds % WEEK == 0) {
88              x = (int)(milliseconds / WEEK);
89  
90              s = x + " Week";
91          }
92          else if (milliseconds % DAY == 0) {
93              x = (int)(milliseconds / DAY);
94  
95              s = x + " Day";
96          }
97          else if (milliseconds % HOUR == 0) {
98              x = (int)(milliseconds / HOUR);
99  
100             s = x + " Hour";
101         }
102         else if (milliseconds % MINUTE == 0) {
103             x = (int)(milliseconds / MINUTE);
104 
105             s = x + " Minute";
106         }
107         else if (milliseconds % SECOND == 0) {
108             x = (int)(milliseconds / SECOND);
109 
110             s = x + " Second";
111         }
112 
113         if (x > 1) {
114             s += "s";
115         }
116 
117         return s;
118     }
119 
120     public static String getRFC822() {
121         return getRFC822(new Date());
122     }
123 
124     public static String getRFC822(Date date) {
125         return getSimpleDate(date, RFC822_FORMAT);
126     }
127 
128     public static String getShortTimestamp() {
129         return getShortTimestamp(new Date());
130     }
131 
132     public static String getShortTimestamp(Date date) {
133         return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
134     }
135 
136     public static String getSimpleDate(Date date, String format) {
137         String s = StringPool.BLANK;
138 
139         if (date != null) {
140             Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
141                 format);
142 
143             s = dateFormat.format(date);
144         }
145 
146         return s;
147     }
148 
149     public static String getTimestamp() {
150         return getTimestamp(new Date());
151     }
152 
153     public static String getTimestamp(Date date) {
154         return getSimpleDate(date, TIMESTAMP_FORMAT);
155     }
156 
157 }