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.util;
24  
25  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.text.SimpleDateFormat;
29  
30  import java.util.Calendar;
31  import java.util.Date;
32  import java.util.TimeZone;
33  
34  /**
35   * <a href="Time.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class Time {
41  
42      public static final long SECOND = 1000;
43  
44      public static final long MINUTE = SECOND * 60;
45  
46      public static final long HOUR = MINUTE * 60;
47  
48      public static final long DAY = HOUR * 24;
49  
50      public static final long WEEK = DAY * 7;
51  
52      public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
53  
54      public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
55  
56      public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
57  
58      public static Date getDate(Calendar cal) {
59          Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
60          adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
61          adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
62          adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
63          adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
64          adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
65          adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
66          adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
67  
68          return adjustedCal.getTime();
69      }
70  
71      public static Date getDate(TimeZone tz) {
72          Calendar cal = CalendarFactoryUtil.getCalendar(tz);
73  
74          return getDate(cal);
75      }
76  
77      public static Date getDate(Date date, TimeZone tz) {
78          Calendar cal = CalendarFactoryUtil.getCalendar(tz);
79          cal.setTime(date);
80  
81          return getDate(cal);
82      }
83  
84      public static String getDescription(long milliseconds) {
85          String s = "";
86  
87          int x = 0;
88  
89          if (milliseconds % WEEK == 0) {
90              x = (int)(milliseconds / WEEK);
91  
92              s = x + " Week";
93          }
94          else if (milliseconds % DAY == 0) {
95              x = (int)(milliseconds / DAY);
96  
97              s = x + " Day";
98          }
99          else if (milliseconds % HOUR == 0) {
100             x = (int)(milliseconds / HOUR);
101 
102             s = x + " Hour";
103         }
104         else if (milliseconds % MINUTE == 0) {
105             x = (int)(milliseconds / MINUTE);
106 
107             s = x + " Minute";
108         }
109         else if (milliseconds % SECOND == 0) {
110             x = (int)(milliseconds / SECOND);
111 
112             s = x + " Second";
113         }
114 
115         if (x > 1) {
116             s += "s";
117         }
118 
119         return s;
120     }
121 
122     public static String getRFC822() {
123         return getRFC822(new Date());
124     }
125 
126     public static String getRFC822(Date date) {
127         return getSimpleDate(date, RFC822_FORMAT);
128     }
129 
130     public static String getShortTimestamp() {
131         return getShortTimestamp(new Date());
132     }
133 
134     public static String getShortTimestamp(Date date) {
135         return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
136     }
137 
138     public static String getSimpleDate(Date date, String format) {
139         String s = StringPool.BLANK;
140 
141         if (date != null) {
142             SimpleDateFormat sdf = new SimpleDateFormat(format);
143 
144             s = sdf.format(date);
145         }
146 
147         return s;
148     }
149 
150     public static String getTimestamp() {
151         return getTimestamp(new Date());
152     }
153 
154     public static String getTimestamp(Date date) {
155         return getSimpleDate(date, TIMESTAMP_FORMAT);
156     }
157 
158 }