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