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