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 long HOUR = Time.MINUTE * 60;
031    
032            public static final long MINUTE = Time.SECOND * 60;
033    
034            public static final long MONTH = Time.DAY * 30;
035    
036            public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
037    
038            public static final long SECOND = 1000;
039    
040            public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
041    
042            public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
043    
044            public static final long WEEK = Time.DAY * 7;
045    
046            public static final long YEAR = Time.DAY * 365;
047    
048            public static Date getDate(Calendar cal) {
049                    Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
050    
051                    adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
052                    adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
053                    adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
054                    adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
055                    adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
056                    adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
057                    adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
058    
059                    return adjustedCal.getTime();
060            }
061    
062            public static Date getDate(Date date, TimeZone tz) {
063                    Calendar cal = CalendarFactoryUtil.getCalendar(tz);
064    
065                    cal.setTime(date);
066    
067                    return getDate(cal);
068            }
069    
070            public static Date getDate(TimeZone tz) {
071                    Calendar cal = CalendarFactoryUtil.getCalendar(tz);
072    
073                    return getDate(cal);
074            }
075    
076            public static String getDescription(long milliseconds) {
077                    return getDescription(milliseconds, false);
078            }
079    
080            public static String getDescription(
081                    long milliseconds, boolean approximate) {
082    
083                    String s = StringPool.BLANK;
084    
085                    int x = 0;
086    
087                    if (approximate) {
088                            if (milliseconds <= 0) {
089                                    s = "0 Seconds";
090                            }
091                            else if (milliseconds < MINUTE) {
092                                    x = (int)(milliseconds / SECOND);
093    
094                                    s = x + " Second";
095                            }
096                            else if (milliseconds < HOUR) {
097                                    x = (int)(milliseconds / MINUTE);
098    
099                                    s = x + " Minute";
100                            }
101                            else if (milliseconds < DAY) {
102                                    x = (int)(milliseconds / HOUR);
103    
104                                    s = x + " Hour";
105                            }
106                            else if (milliseconds < MONTH) {
107                                    x = (int)(milliseconds / DAY);
108    
109                                    s = x + " Day";
110                            }
111                            else if (milliseconds < YEAR) {
112                                    x = (int)(milliseconds / MONTH);
113    
114                                    s = x + " Month";
115                            }
116                            else if (milliseconds >= YEAR) {
117                                    x = (int)(milliseconds / YEAR);
118    
119                                    s = x + " Year";
120                            }
121                    }
122                    else {
123                            if (milliseconds % WEEK == 0) {
124                                    x = (int)(milliseconds / WEEK);
125    
126                                    s = x + " Week";
127                            }
128                            else if (milliseconds % DAY == 0) {
129                                    x = (int)(milliseconds / DAY);
130    
131                                    s = x + " Day";
132                            }
133                            else if (milliseconds % HOUR == 0) {
134                                    x = (int)(milliseconds / HOUR);
135    
136                                    s = x + " Hour";
137                            }
138                            else if (milliseconds % MINUTE == 0) {
139                                    x = (int)(milliseconds / MINUTE);
140    
141                                    s = x + " Minute";
142                            }
143                            else if (milliseconds % SECOND == 0) {
144                                    x = (int)(milliseconds / SECOND);
145    
146                                    s = x + " Second";
147                            }
148                            else {
149                                    x = (int)milliseconds;
150    
151                                    s = x + " Millisecond";
152                            }
153                    }
154    
155                    if (x > 1) {
156                            s += "s";
157                    }
158    
159                    return s;
160            }
161    
162            public static String getRFC822() {
163                    return getRFC822(new Date());
164            }
165    
166            public static String getRFC822(Date date) {
167                    return getSimpleDate(date, RFC822_FORMAT);
168            }
169    
170            public static String getShortTimestamp() {
171                    return getShortTimestamp(new Date());
172            }
173    
174            public static String getShortTimestamp(Date date) {
175                    return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
176            }
177    
178            public static String getSimpleDate(Date date, String format) {
179                    String s = StringPool.BLANK;
180    
181                    if (date != null) {
182                            Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
183                                    format);
184    
185                            s = dateFormat.format(date);
186                    }
187    
188                    return s;
189            }
190    
191            public static String getTimestamp() {
192                    return getTimestamp(new Date());
193            }
194    
195            public static String getTimestamp(Date date) {
196                    return getSimpleDate(date, TIMESTAMP_FORMAT);
197            }
198    
199    }