1
19
20 package com.liferay.portal.kernel.util;
21
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.Locale;
29 import java.util.TimeZone;
30
31
37 public class DateUtil {
38
39 public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
40
41 public static int compareTo(Date date1, Date date2) {
42
43
47 if ((date1 != null) && (date2 == null)) {
48 return -1;
49 }
50 else if ((date1 == null) && (date2 != null)) {
51 return 1;
52 }
53 else if ((date1 == null) && (date2 == null)) {
54 return 0;
55 }
56
57 long time1 = date1.getTime();
58 long time2 = date2.getTime();
59
60 if (time1 == time2) {
61 return 0;
62 }
63 else if (time1 < time2) {
64 return -1;
65 }
66 else {
67 return 1;
68 }
69 }
70
71 public static boolean equals(Date date1, Date date2) {
72 if (compareTo(date1, date2) == 0) {
73 return true;
74 }
75 else {
76 return false;
77 }
78 }
79
80 public static boolean equals(
81 Date date1, Date date2, boolean ignoreMilliseconds) {
82
83 if (!ignoreMilliseconds) {
84 return equals(date1, date2);
85 }
86
87 long time1 = 0;
88
89 if (date1 != null) {
90 time1 = date1.getTime() / Time.SECOND;
91 }
92
93 long time2 = 0;
94
95 if (date2 != null) {
96 time2 = date2.getTime() / Time.SECOND;
97 }
98
99 if (time1 == time2) {
100 return true;
101 }
102 else {
103 return false;
104 }
105 }
106
107 public static String getCurrentDate(String pattern, Locale locale) {
108 return getDate(new Date(), pattern, locale);
109 }
110
111 public static String getCurrentDate(
112 String pattern, Locale locale, TimeZone timeZone) {
113
114 return getDate(new Date(), pattern, locale, timeZone);
115 }
116
117 public static String getDate(Date date, String pattern, Locale locale) {
118 DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
119
120 return dateFormat.format(date);
121 }
122
123 public static String getDate(
124 Date date, String pattern, Locale locale, TimeZone timeZone) {
125
126 DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
127
128 dateFormat.setTimeZone(timeZone);
129
130 return dateFormat.format(date);
131 }
132
133 public static int getDaysBetween(
134 Date startDate, Date endDate, TimeZone timeZone) {
135
136 int offset = timeZone.getRawOffset();
137
138 Calendar startCal = new GregorianCalendar(timeZone);
139
140 startCal.setTime(startDate);
141 startCal.add(Calendar.MILLISECOND, offset);
142
143 Calendar endCal = new GregorianCalendar(timeZone);
144
145 endCal.setTime(endDate);
146 endCal.add(Calendar.MILLISECOND, offset);
147
148 int daysBetween = 0;
149
150 while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
151 startCal.add(Calendar.DAY_OF_MONTH, 1);
152
153 daysBetween++;
154 }
155
156 return daysBetween;
157 }
158
159 public static DateFormat getISOFormat() {
160 return getISOFormat(StringPool.BLANK);
161 }
162
163 public static DateFormat getISOFormat(String text) {
164 String pattern = StringPool.BLANK;
165
166 if (text.length() == 8) {
167 pattern = "yyyyMMdd";
168 }
169 else if (text.length() == 12) {
170 pattern = "yyyyMMddHHmm";
171 }
172 else if (text.length() == 13) {
173 pattern = "yyyyMMdd'T'HHmm";
174 }
175 else if (text.length() == 14) {
176 pattern = "yyyyMMddHHmmss";
177 }
178 else if (text.length() == 15) {
179 pattern = "yyyyMMdd'T'HHmmss";
180 }
181 else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
182 pattern = "yyyyMMdd'T'HHmmssz";
183 }
184 else {
185 pattern = "yyyyMMddHHmmssz";
186 }
187
188 return new SimpleDateFormat(pattern);
189 }
190
191 public static DateFormat getISO8601Format() {
192 return new SimpleDateFormat(ISO_8601_PATTERN);
193 }
194
195 public static DateFormat getUTCFormat() {
196 return getUTCFormat(StringPool.BLANK);
197 }
198
199 public static DateFormat getUTCFormat(String text) {
200 String pattern = StringPool.BLANK;
201
202 if (text.length() == 8) {
203 pattern = "yyyyMMdd";
204 }
205 else if (text.length() == 12) {
206 pattern = "yyyyMMddHHmm";
207 }
208 else if (text.length() == 13) {
209 pattern = "yyyyMMdd'T'HHmm";
210 }
211 else if (text.length() == 14) {
212 pattern = "yyyyMMddHHmmss";
213 }
214 else if (text.length() == 15) {
215 pattern = "yyyyMMdd'T'HHmmss";
216 }
217 else {
218 pattern = "yyyyMMdd'T'HHmmssz";
219 }
220
221 DateFormat dateFormat = new SimpleDateFormat(pattern);
222
223 dateFormat.setTimeZone(TimeZone.getTimeZone(StringPool.UTC));
224
225 return dateFormat;
226 }
227
228 public static Date newDate() {
229 return new Date();
230 }
231
232 public static Date newDate(long date) {
233 return new Date(date);
234 }
235
236 }