001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025:
026: import java.text.SimpleDateFormat;
027:
028: import java.util.Calendar;
029: import java.util.Date;
030: import java.util.TimeZone;
031:
032: /**
033: * <a href="Time.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class Time {
039:
040: public static final long SECOND = 1000;
041:
042: public static final long MINUTE = SECOND * 60;
043:
044: public static final long HOUR = MINUTE * 60;
045:
046: public static final long DAY = HOUR * 24;
047:
048: public static final long WEEK = DAY * 7;
049:
050: public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
051:
052: public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
053:
054: public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
055:
056: public static Date getDate(Calendar cal) {
057: Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
058: adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
059: adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
060: adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
061: adjustedCal.set(Calendar.HOUR_OF_DAY, cal
062: .get(Calendar.HOUR_OF_DAY));
063: adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
064: adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
065: adjustedCal.set(Calendar.MILLISECOND, cal
066: .get(Calendar.MILLISECOND));
067:
068: return adjustedCal.getTime();
069: }
070:
071: public static Date getDate(TimeZone tz) {
072: Calendar cal = CalendarFactoryUtil.getCalendar(tz);
073:
074: return getDate(cal);
075: }
076:
077: public static Date getDate(Date date, TimeZone tz) {
078: Calendar cal = CalendarFactoryUtil.getCalendar(tz);
079: cal.setTime(date);
080:
081: return getDate(cal);
082: }
083:
084: public static String getDescription(long milliseconds) {
085: String s = "";
086:
087: int x = 0;
088:
089: if (milliseconds % WEEK == 0) {
090: x = (int) (milliseconds / WEEK);
091:
092: s = x + " Week";
093: } else if (milliseconds % DAY == 0) {
094: x = (int) (milliseconds / DAY);
095:
096: s = x + " Day";
097: } else if (milliseconds % HOUR == 0) {
098: x = (int) (milliseconds / HOUR);
099:
100: s = x + " Hour";
101: } else if (milliseconds % MINUTE == 0) {
102: x = (int) (milliseconds / MINUTE);
103:
104: s = x + " Minute";
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: }
|