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