001
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
026 public class Time {
027
028 public static final long SECOND = 1000;
029
030 public static final long MINUTE = SECOND * 60;
031
032 public static final long HOUR = MINUTE * 60;
033
034 public static final long DAY = HOUR * 24;
035
036 public static final long WEEK = DAY * 7;
037
038 public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
039
040 public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
041
042 public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
043
044 public static Date getDate(Calendar cal) {
045 Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
046
047 adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
048 adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
049 adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
050 adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
051 adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
052 adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
053 adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
054
055 return adjustedCal.getTime();
056 }
057
058 public static Date getDate(TimeZone tz) {
059 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
060
061 return getDate(cal);
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 String getDescription(long milliseconds) {
073 String s = StringPool.BLANK;
074
075 int x = 0;
076
077 if (milliseconds % WEEK == 0) {
078 x = (int)(milliseconds / WEEK);
079
080 s = x + " Week";
081 }
082 else if (milliseconds % DAY == 0) {
083 x = (int)(milliseconds / DAY);
084
085 s = x + " Day";
086 }
087 else if (milliseconds % HOUR == 0) {
088 x = (int)(milliseconds / HOUR);
089
090 s = x + " Hour";
091 }
092 else if (milliseconds % MINUTE == 0) {
093 x = (int)(milliseconds / MINUTE);
094
095 s = x + " Minute";
096 }
097 else if (milliseconds % SECOND == 0) {
098 x = (int)(milliseconds / SECOND);
099
100 s = x + " Second";
101 }
102
103 if (x > 1) {
104 s += "s";
105 }
106
107 return s;
108 }
109
110 public static String getRFC822() {
111 return getRFC822(new Date());
112 }
113
114 public static String getRFC822(Date date) {
115 return getSimpleDate(date, RFC822_FORMAT);
116 }
117
118 public static String getShortTimestamp() {
119 return getShortTimestamp(new Date());
120 }
121
122 public static String getShortTimestamp(Date date) {
123 return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
124 }
125
126 public static String getSimpleDate(Date date, String format) {
127 String s = StringPool.BLANK;
128
129 if (date != null) {
130 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
131 format);
132
133 s = dateFormat.format(date);
134 }
135
136 return s;
137 }
138
139 public static String getTimestamp() {
140 return getTimestamp(new Date());
141 }
142
143 public static String getTimestamp(Date date) {
144 return getSimpleDate(date, TIMESTAMP_FORMAT);
145 }
146
147 }