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  
55          adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
56          adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
57          adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
58          adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
59          adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
60          adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
61          adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
62  
63          return adjustedCal.getTime();
64      }
65  
66      public static Date getDate(TimeZone tz) {
67          Calendar cal = CalendarFactoryUtil.getCalendar(tz);
68  
69          return getDate(cal);
70      }
71  
72      public static Date getDate(Date date, TimeZone tz) {
73          Calendar cal = CalendarFactoryUtil.getCalendar(tz);
74  
75          cal.setTime(date);
76  
77          return getDate(cal);
78      }
79  
80      public static String getDescription(long milliseconds) {
81          String s = "";
82  
83          int x = 0;
84  
85          if (milliseconds % WEEK == 0) {
86              x = (int)(milliseconds / WEEK);
87  
88              s = x + " Week";
89          }
90          else if (milliseconds % DAY == 0) {
91              x = (int)(milliseconds / DAY);
92  
93              s = x + " Day";
94          }
95          else if (milliseconds % HOUR == 0) {
96              x = (int)(milliseconds / HOUR);
97  
98              s = x + " Hour";
99          }
100         else if (milliseconds % MINUTE == 0) {
101             x = (int)(milliseconds / MINUTE);
102 
103             s = x + " Minute";
104         }
105         else if (milliseconds % SECOND == 0) {
106             x = (int)(milliseconds / SECOND);
107 
108             s = x + " Second";
109         }
110 
111         if (x > 1) {
112             s += "s";
113         }
114 
115         return s;
116     }
117 
118     public static String getRFC822() {
119         return getRFC822(new Date());
120     }
121 
122     public static String getRFC822(Date date) {
123         return getSimpleDate(date, RFC822_FORMAT);
124     }
125 
126     public static String getShortTimestamp() {
127         return getShortTimestamp(new Date());
128     }
129 
130     public static String getShortTimestamp(Date date) {
131         return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
132     }
133 
134     public static String getSimpleDate(Date date, String format) {
135         String s = StringPool.BLANK;
136 
137         if (date != null) {
138             SimpleDateFormat sdf = new SimpleDateFormat(format);
139 
140             s = sdf.format(date);
141         }
142 
143         return s;
144     }
145 
146     public static String getTimestamp() {
147         return getTimestamp(new Date());
148     }
149 
150     public static String getTimestamp(Date date) {
151         return getSimpleDate(date, TIMESTAMP_FORMAT);
152     }
153 
154 }