1
19
20 package com.liferay.portal.kernel.util;
21
22 import java.text.SimpleDateFormat;
23
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.TimeZone;
27
28
34 public class Time {
35
36 public static final long SECOND = 1000;
37
38 public static final long MINUTE = SECOND * 60;
39
40 public static final long HOUR = MINUTE * 60;
41
42 public static final long DAY = HOUR * 24;
43
44 public static final long WEEK = DAY * 7;
45
46 public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
47
48 public static final String TIMESTAMP_FORMAT = "yyyyMMddkkmmssSSS";
49
50 public static final String SHORT_TIMESTAMP_FORMAT = "yyyyMMddkkmm";
51
52 public static Date getDate(Calendar cal) {
53 Calendar adjustedCal = CalendarFactoryUtil.getCalendar();
54
55 adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
56 adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
57 adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
58 adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
59 adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
60 adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
61 adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
62
63 return adjustedCal.getTime();
64 }
65
66 public static Date getDate(TimeZone tz) {
67 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
68
69 return getDate(cal);
70 }
71
72 public static Date getDate(Date date, TimeZone tz) {
73 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
74
75 cal.setTime(date);
76
77 return getDate(cal);
78 }
79
80 public static String getDescription(long milliseconds) {
81 String s = "";
82
83 int x = 0;
84
85 if (milliseconds % WEEK == 0) {
86 x = (int)(milliseconds / WEEK);
87
88 s = x + " Week";
89 }
90 else if (milliseconds % DAY == 0) {
91 x = (int)(milliseconds / DAY);
92
93 s = x + " Day";
94 }
95 else if (milliseconds % HOUR == 0) {
96 x = (int)(milliseconds / HOUR);
97
98 s = x + " Hour";
99 }
100 else if (milliseconds % MINUTE == 0) {
101 x = (int)(milliseconds / MINUTE);
102
103 s = x + " Minute";
104 }
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 }