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.SimpleDateFormat;
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   */
37  public class Time {
38  
39      public static final long SECOND = 1000;
40  
41      public static final long MINUTE = SECOND * 60;
42  
43      public static final long HOUR = MINUTE * 60;
44  
45      public static final long DAY = HOUR * 24;
46  
47      public static final long WEEK = DAY * 7;
48  
49      public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
50  
51      public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
52  
53      public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
54  
55      public static Date getDate(Calendar cal) {
56          Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
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          cal.setTime(date);
77  
78          return getDate(cal);
79      }
80  
81      public static String getDescription(long milliseconds) {
82          String s = "";
83  
84          int x = 0;
85  
86          if (milliseconds % WEEK == 0) {
87              x = (int)(milliseconds / WEEK);
88  
89              s = x + " Week";
90          }
91          else if (milliseconds % DAY == 0) {
92              x = (int)(milliseconds / DAY);
93  
94              s = x + " Day";
95          }
96          else if (milliseconds % HOUR == 0) {
97              x = (int)(milliseconds / HOUR);
98  
99              s = x + " Hour";
100         }
101         else if (milliseconds % MINUTE == 0) {
102             x = (int)(milliseconds / MINUTE);
103 
104             s = x + " Minute";
105         }
106         else if (milliseconds % SECOND == 0) {
107             x = (int)(milliseconds / SECOND);
108 
109             s = x + " Second";
110         }
111 
112         if (x > 1) {
113             s += "s";
114         }
115 
116         return s;
117     }
118 
119     public static String getRFC822() {
120         return getRFC822(new Date());
121     }
122 
123     public static String getRFC822(Date date) {
124         return getSimpleDate(date, RFC822_FORMAT);
125     }
126 
127     public static String getShortTimestamp() {
128         return getShortTimestamp(new Date());
129     }
130 
131     public static String getShortTimestamp(Date date) {
132         return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
133     }
134 
135     public static String getSimpleDate(Date date, String format) {
136         String s = StringPool.BLANK;
137 
138         if (date != null) {
139             SimpleDateFormat sdf = new SimpleDateFormat(format);
140 
141             s = sdf.format(date);
142         }
143 
144         return s;
145     }
146 
147     public static String getTimestamp() {
148         return getTimestamp(new Date());
149     }
150 
151     public static String getTimestamp(Date date) {
152         return getSimpleDate(date, TIMESTAMP_FORMAT);
153     }
154 
155 }