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