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 adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
58 adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
59 adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
60 adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
61 adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
62 adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
63 adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
64
65 return adjustedCal.getTime();
66 }
67
68 public static Date getDate(TimeZone tz) {
69 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
70
71 return getDate(cal);
72 }
73
74 public static Date getDate(Date date, TimeZone tz) {
75 Calendar cal = CalendarFactoryUtil.getCalendar(tz);
76 cal.setTime(date);
77
78 return getDate(cal);
79 }
80
81 public static String getDescription(long milliseconds) {
82 String s = "";
83
84 int x = 0;
85
86 if (milliseconds % WEEK == 0) {
87 x = (int)(milliseconds / WEEK);
88
89 s = x + " Week";
90 }
91 else if (milliseconds % DAY == 0) {
92 x = (int)(milliseconds / DAY);
93
94 s = x + " Day";
95 }
96 else if (milliseconds % HOUR == 0) {
97 x = (int)(milliseconds / HOUR);
98
99 s = x + " Hour";
100 }
101 else if (milliseconds % MINUTE == 0) {
102 x = (int)(milliseconds / MINUTE);
103
104 s = x + " Minute";
105 }
106 else if (milliseconds % SECOND == 0) {
107 x = (int)(milliseconds / SECOND);
108
109 s = x + " Second";
110 }
111
112 if (x > 1) {
113 s += "s";
114 }
115
116 return s;
117 }
118
119 public static String getRFC822() {
120 return getRFC822(new Date());
121 }
122
123 public static String getRFC822(Date date) {
124 return getSimpleDate(date, RFC822_FORMAT);
125 }
126
127 public static String getShortTimestamp() {
128 return getShortTimestamp(new Date());
129 }
130
131 public static String getShortTimestamp(Date date) {
132 return getSimpleDate(date, SHORT_TIMESTAMP_FORMAT);
133 }
134
135 public static String getSimpleDate(Date date, String format) {
136 String s = StringPool.BLANK;
137
138 if (date != null) {
139 SimpleDateFormat sdf = new SimpleDateFormat(format);
140
141 s = sdf.format(date);
142 }
143
144 return s;
145 }
146
147 public static String getTimestamp() {
148 return getTimestamp(new Date());
149 }
150
151 public static String getTimestamp(Date date) {
152 return getSimpleDate(date, TIMESTAMP_FORMAT);
153 }
154
155 }