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