001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.text.DateFormat;
018 import java.text.Format;
019 import java.text.ParseException;
020 import java.text.SimpleDateFormat;
021
022 import java.util.Calendar;
023 import java.util.Date;
024 import java.util.GregorianCalendar;
025 import java.util.HashMap;
026 import java.util.Locale;
027 import java.util.Map;
028 import java.util.TimeZone;
029
030
033 public class DateUtil {
034
035 public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
036
037 public static int compareTo(Date date1, Date date2) {
038 if (date1 == date2) {
039 return 0;
040 }
041
042 if (date1 == null) {
043 return 1;
044 }
045
046 if (date2 == null) {
047 return -1;
048 }
049
050 return Long.compare(date1.getTime(), date2.getTime());
051 }
052
053 public static boolean equals(Date date1, Date date2) {
054 if (compareTo(date1, date2) == 0) {
055 return true;
056 }
057
058 return false;
059 }
060
061 public static String formatDate(
062 String fromPattern, String dateString, Locale locale)
063 throws ParseException {
064
065 Date dateValue = parseDate(fromPattern, dateString, locale);
066
067 Format dateFormat = FastDateFormatFactoryUtil.getDate(locale);
068
069 return dateFormat.format(dateValue);
070 }
071
072 public static String getCurrentDate(String pattern, Locale locale) {
073 return getDate(new Date(), pattern, locale);
074 }
075
076 public static String getCurrentDate(
077 String pattern, Locale locale, TimeZone timeZone) {
078
079 return getDate(new Date(), pattern, locale, timeZone);
080 }
081
082 public static String getDate(Date date, String pattern, Locale locale) {
083 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
084 pattern, locale);
085
086 return dateFormat.format(date);
087 }
088
089 public static String getDate(
090 Date date, String pattern, Locale locale, TimeZone timeZone) {
091
092 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
093 pattern, locale, timeZone);
094
095 return dateFormat.format(date);
096 }
097
098 public static int getDaysBetween(Date date1, Date date2) {
099 return getDaysBetween(date1, date2, null);
100 }
101
102 public static int getDaysBetween(
103 Date date1, Date date2, TimeZone timeZone) {
104
105 if (date1.after(date2)) {
106 Date tempDate = date1;
107
108 date1 = date2;
109 date2 = tempDate;
110 }
111
112 Calendar startCal = null;
113 Calendar endCal = null;
114
115 int offsetDate1 = 0;
116 int offsetDate2 = 0;
117
118 if (timeZone == null) {
119 startCal = new GregorianCalendar();
120 endCal = new GregorianCalendar();
121 }
122 else {
123 startCal = new GregorianCalendar(timeZone);
124 endCal = new GregorianCalendar(timeZone);
125
126 offsetDate1 = timeZone.getOffset(date1.getTime());
127 offsetDate2 = timeZone.getOffset(date2.getTime());
128 }
129
130 startCal.setTime(date1);
131
132 startCal.add(Calendar.MILLISECOND, offsetDate1);
133
134 endCal.setTime(date2);
135
136 endCal.add(Calendar.MILLISECOND, offsetDate2);
137
138 int daysBetween = 0;
139
140 while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
141 startCal.add(Calendar.DAY_OF_MONTH, 1);
142
143 daysBetween++;
144 }
145
146 return daysBetween;
147 }
148
149 public static DateFormat getISO8601Format() {
150 return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
151 }
152
153 public static DateFormat getISOFormat() {
154 return getISOFormat(StringPool.BLANK);
155 }
156
157 public static DateFormat getISOFormat(String text) {
158 String pattern = StringPool.BLANK;
159
160 if (text.length() == 8) {
161 pattern = "yyyyMMdd";
162 }
163 else if (text.length() == 12) {
164 pattern = "yyyyMMddHHmm";
165 }
166 else if (text.length() == 13) {
167 pattern = "yyyyMMdd'T'HHmm";
168 }
169 else if (text.length() == 14) {
170 pattern = "yyyyMMddHHmmss";
171 }
172 else if (text.length() == 15) {
173 pattern = "yyyyMMdd'T'HHmmss";
174 }
175 else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
176 pattern = "yyyyMMdd'T'HHmmssz";
177 }
178 else {
179 pattern = "yyyyMMddHHmmssz";
180 }
181
182 return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
183 }
184
185 public static DateFormat getUTCFormat() {
186 return getUTCFormat(StringPool.BLANK);
187 }
188
189 public static DateFormat getUTCFormat(String text) {
190 String pattern = StringPool.BLANK;
191
192 if (text.length() == 8) {
193 pattern = "yyyyMMdd";
194 }
195 else if (text.length() == 12) {
196 pattern = "yyyyMMddHHmm";
197 }
198 else if (text.length() == 13) {
199 pattern = "yyyyMMdd'T'HHmm";
200 }
201 else if (text.length() == 14) {
202 pattern = "yyyyMMddHHmmss";
203 }
204 else if (text.length() == 15) {
205 pattern = "yyyyMMdd'T'HHmmss";
206 }
207 else {
208 pattern = "yyyyMMdd'T'HHmmssz";
209 }
210
211 return DateFormatFactoryUtil.getSimpleDateFormat(
212 pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
213 }
214
215 public static boolean isFormatAmPm(Locale locale) {
216 Boolean formatAmPm = _formatAmPmMap.get(locale);
217
218 if (formatAmPm == null) {
219 SimpleDateFormat simpleDateFormat =
220 (SimpleDateFormat)DateFormat.getTimeInstance(
221 DateFormat.SHORT, locale);
222
223 String pattern = simpleDateFormat.toPattern();
224
225 formatAmPm = pattern.contains("a");
226
227 _formatAmPmMap.put(locale, formatAmPm);
228 }
229
230 return formatAmPm;
231 }
232
233 public static Date newDate() {
234 return new Date();
235 }
236
237 public static Date newDate(long date) {
238 return new Date(date);
239 }
240
241 public static long newTime() {
242 Date date = new Date();
243
244 return date.getTime();
245 }
246
247 public static Date parseDate(String dateString, Locale locale)
248 throws ParseException {
249
250 return parseDate(null, dateString, locale);
251 }
252
253 public static Date parseDate(
254 String pattern, String dateString, Locale locale)
255 throws ParseException {
256
257 DateFormat dateFormat = null;
258
259 if (Validator.isNull(pattern)) {
260 dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
261 }
262 else {
263 dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
264 pattern, locale);
265 }
266
267 return dateFormat.parse(dateString);
268 }
269
270 private static final Map<Locale, Boolean> _formatAmPmMap = new HashMap<>();
271
272 }