001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.text.Format;
018    
019    import java.util.Calendar;
020    import java.util.Date;
021    import java.util.TimeZone;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class Time {
027    
028            public static final long DAY = Time.HOUR * 24;
029    
030            public static final String DURATION_FORMAT = "HH:mm:ss.SSS";
031    
032            public static final long HOUR = Time.MINUTE * 60;
033    
034            public static final long MINUTE = Time.SECOND * 60;
035    
036            public static final long MONTH = Time.DAY * 30;
037    
038            public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
039    
040            public static final long SECOND = 1000;
041    
042            public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
043    
044            public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
045    
046            public static final long WEEK = Time.DAY * 7;
047    
048            public static final long YEAR = Time.DAY * 365;
049    
050            public static Date getDate(Calendar cal) {
051                    Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
052    
053                    adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
054                    adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
055                    adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
056                    adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
057                    adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
058                    adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
059                    adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
060    
061                    return adjustedCal.getTime();
062            }
063    
064            public static Date getDate(Date date, TimeZone tz) {
065                    Calendar cal = CalendarFactoryUtil.getCalendar(tz);
066    
067                    cal.setTime(date);
068    
069                    return getDate(cal);
070            }
071    
072            public static Date getDate(TimeZone tz) {
073                    Calendar cal = CalendarFactoryUtil.getCalendar(tz);
074    
075                    return getDate(cal);
076            }
077    
078            public static String getDescription(long milliseconds) {
079                    return getDescription(milliseconds, false);
080            }
081    
082            public static String getDescription(
083                    long milliseconds, boolean approximate) {
084    
085                    String s = StringPool.BLANK;
086    
087                    int x = 0;
088    
089                    if (approximate) {
090                            if (milliseconds <= 0) {
091                                    s = "0 Seconds";
092                            }
093                            else if (milliseconds < MINUTE) {
094                                    x = (int)(milliseconds / SECOND);
095    
096                                    s = x + " Second";
097                            }
098                            else if (milliseconds < HOUR) {
099                                    x = (int)(milliseconds / MINUTE);
100    
101                                    s = x + " Minute";
102                            }
103                            else if (milliseconds < DAY) {
104                                    x = (int)(milliseconds / HOUR);
105    
106                                    s = x + " Hour";
107                            }
108                            else if (milliseconds < MONTH) {
109                                    x = (int)(milliseconds / DAY);
110    
111                                    s = x + " Day";
112                            }
113                            else if (milliseconds < YEAR) {
114                                    x = (int)(milliseconds / MONTH);
115    
116                                    s = x + " Month";
117                            }
118                            else if (milliseconds >= YEAR) {
119                                    x = (int)(milliseconds / YEAR);
120    
121                                    s = x + " Year";
122                            }
123                    }
124                    else {
125                            if ((milliseconds % WEEK) == 0) {
126                                    x = (int)(milliseconds / WEEK);
127    
128                                    s = x + " Week";
129                            }
130                            else if ((milliseconds % DAY) == 0) {
131                                    x = (int)(milliseconds / DAY);
132    
133                                    s = x + " Day";
134                            }
135                            else if ((milliseconds % HOUR) == 0) {
136                                    x = (int)(milliseconds / HOUR);
137    
138                                    s = x + " Hour";
139                            }
140                            else if ((milliseconds % MINUTE) == 0) {
141                                    x = (int)(milliseconds / MINUTE);
142    
143                                    s = x + " Minute";
144                            }
145                            else if ((milliseconds % SECOND) == 0) {
146                                    x = (int)(milliseconds / SECOND);
147    
148                                    s = x + " Second";
149                            }
150                            else {
151                                    x = (int)milliseconds;
152    
153                                    s = x + " Millisecond";
154                            }
155                    }
156    
157                    if (x > 1) {
158                            s += "s";
159                    }
160    
161                    return s;
162            }
163    
164            public static String getDuration(long milliseconds) {
165                    return getSimpleDate(new Date(milliseconds), DURATION_FORMAT);
166            }
167    
168            public static String getRFC822() {
169                    return getRFC822(new Date());
170            }
171    
172            public static String getRFC822(Date date) {
173                    return getSimpleDate(date, RFC822_FORMAT);
174            }
175    
176            public static String getShortTimestamp() {
177                    return getShortTimestamp(new Date());
178            }
179    
180            public static String getShortTimestamp(Date date) {
181                    return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
182            }
183    
184            public static String getSimpleDate(Date date, String format) {
185                    String s = StringPool.BLANK;
186    
187                    if (date != null) {
188                            Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
189                                    format);
190    
191                            s = dateFormat.format(date);
192                    }
193    
194                    return s;
195            }
196    
197            public static String getTimestamp() {
198                    return getTimestamp(new Date());
199            }
200    
201            public static String getTimestamp(Date date) {
202                    return getSimpleDate(date, TIMESTAMP_FORMAT);
203            }
204    
205    }