1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27
28 import java.text.SimpleDateFormat;
29
30 import java.util.Calendar;
31 import java.util.Date;
32 import java.util.TimeZone;
33
34
40 public class Time {
41
42 public static final long SECOND = 1000;
43
44 public static final long MINUTE = SECOND * 60;
45
46 public static final long HOUR = MINUTE * 60;
47
48 public static final long DAY = HOUR * 24;
49
50 public static final long WEEK = DAY * 7;
51
52 public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
53
54 public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
55
56 public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
57
58 public static Date getDate(Calendar cal) {
59 Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
60 adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
61 adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
62 adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
63 adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
64 adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
65 adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
66 adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
67
68 return adjustedCal.getTime();
69 }
70
71 public static Date getDate(TimeZone tz) {
72 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
73
74 return getDate(cal);
75 }
76
77 public static Date getDate(Date date, TimeZone tz) {
78 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
79 cal.setTime(date);
80
81 return getDate(cal);
82 }
83
84 public static String getDescription(long milliseconds) {
85 String s = "";
86
87 int x = 0;
88
89 if (milliseconds % WEEK == 0) {
90 x = (int)(milliseconds / WEEK);
91
92 s = x + " Week";
93 }
94 else if (milliseconds % DAY == 0) {
95 x = (int)(milliseconds / DAY);
96
97 s = x + " Day";
98 }
99 else if (milliseconds % HOUR == 0) {
100 x = (int)(milliseconds / HOUR);
101
102 s = x + " Hour";
103 }
104 else if (milliseconds % MINUTE == 0) {
105 x = (int)(milliseconds / MINUTE);
106
107 s = x + " Minute";
108 }
109 else if (milliseconds % SECOND == 0) {
110 x = (int)(milliseconds / SECOND);
111
112 s = x + " Second";
113 }
114
115 if (x > 1) {
116 s += "s";
117 }
118
119 return s;
120 }
121
122 public static String getRFC822() {
123 return getRFC822(new Date());
124 }
125
126 public static String getRFC822(Date date) {
127 return getSimpleDate(date, RFC822_FORMAT);
128 }
129
130 public static String getShortTimestamp() {
131 return getShortTimestamp(new Date());
132 }
133
134 public static String getShortTimestamp(Date date) {
135 return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
136 }
137
138 public static String getSimpleDate(Date date, String format) {
139 String s = StringPool.BLANK;
140
141 if (date != null) {
142 SimpleDateFormat sdf = new SimpleDateFormat(format);
143
144 s = sdf.format(date);
145 }
146
147 return s;
148 }
149
150 public static String getTimestamp() {
151 return getTimestamp(new Date());
152 }
153
154 public static String getTimestamp(Date date) {
155 return getSimpleDate(date, TIMESTAMP_FORMAT);
156 }
157
158 }