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 getCurrentDate(String pattern, Locale locale) {
062 return getDate(new Date(), pattern, locale);
063 }
064
065 public static String getCurrentDate(
066 String pattern, Locale locale, TimeZone timeZone) {
067
068 return getDate(new Date(), pattern, locale, timeZone);
069 }
070
071 public static String getDate(Date date, String pattern, Locale locale) {
072 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
073 pattern, locale);
074
075 return dateFormat.format(date);
076 }
077
078 public static String getDate(
079 Date date, String pattern, Locale locale, TimeZone timeZone) {
080
081 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
082 pattern, locale, timeZone);
083
084 return dateFormat.format(date);
085 }
086
087 public static int getDaysBetween(Date date1, Date date2) {
088 return getDaysBetween(date1, date2, null);
089 }
090
091 public static int getDaysBetween(
092 Date date1, Date date2, TimeZone timeZone) {
093
094 if (date1.after(date2)) {
095 Date tempDate = date1;
096
097 date1 = date2;
098 date2 = tempDate;
099 }
100
101 Calendar startCal = null;
102 Calendar endCal = null;
103
104 int offsetDate1 = 0;
105 int offsetDate2 = 0;
106
107 if (timeZone == null) {
108 startCal = new GregorianCalendar();
109 endCal = new GregorianCalendar();
110 }
111 else {
112 startCal = new GregorianCalendar(timeZone);
113 endCal = new GregorianCalendar(timeZone);
114
115 offsetDate1 = timeZone.getOffset(date1.getTime());
116 offsetDate2 = timeZone.getOffset(date2.getTime());
117 }
118
119 startCal.setTime(date1);
120
121 startCal.add(Calendar.MILLISECOND, offsetDate1);
122
123 endCal.setTime(date2);
124
125 endCal.add(Calendar.MILLISECOND, offsetDate2);
126
127 int daysBetween = 0;
128
129 while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
130 startCal.add(Calendar.DAY_OF_MONTH, 1);
131
132 daysBetween++;
133 }
134
135 return daysBetween;
136 }
137
138 public static DateFormat getISO8601Format() {
139 return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
140 }
141
142 public static DateFormat getISOFormat() {
143 return getISOFormat(StringPool.BLANK);
144 }
145
146 public static DateFormat getISOFormat(String text) {
147 String pattern = StringPool.BLANK;
148
149 if (text.length() == 8) {
150 pattern = "yyyyMMdd";
151 }
152 else if (text.length() == 12) {
153 pattern = "yyyyMMddHHmm";
154 }
155 else if (text.length() == 13) {
156 pattern = "yyyyMMdd'T'HHmm";
157 }
158 else if (text.length() == 14) {
159 pattern = "yyyyMMddHHmmss";
160 }
161 else if (text.length() == 15) {
162 pattern = "yyyyMMdd'T'HHmmss";
163 }
164 else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
165 pattern = "yyyyMMdd'T'HHmmssz";
166 }
167 else {
168 pattern = "yyyyMMddHHmmssz";
169 }
170
171 return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
172 }
173
174 public static DateFormat getUTCFormat() {
175 return getUTCFormat(StringPool.BLANK);
176 }
177
178 public static DateFormat getUTCFormat(String text) {
179 String pattern = StringPool.BLANK;
180
181 if (text.length() == 8) {
182 pattern = "yyyyMMdd";
183 }
184 else if (text.length() == 12) {
185 pattern = "yyyyMMddHHmm";
186 }
187 else if (text.length() == 13) {
188 pattern = "yyyyMMdd'T'HHmm";
189 }
190 else if (text.length() == 14) {
191 pattern = "yyyyMMddHHmmss";
192 }
193 else if (text.length() == 15) {
194 pattern = "yyyyMMdd'T'HHmmss";
195 }
196 else {
197 pattern = "yyyyMMdd'T'HHmmssz";
198 }
199
200 return DateFormatFactoryUtil.getSimpleDateFormat(
201 pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
202 }
203
204 public static boolean isFormatAmPm(Locale locale) {
205 Boolean formatAmPm = _formatAmPmMap.get(locale);
206
207 if (formatAmPm == null) {
208 SimpleDateFormat simpleDateFormat =
209 (SimpleDateFormat)DateFormat.getTimeInstance(
210 DateFormat.SHORT, locale);
211
212 String pattern = simpleDateFormat.toPattern();
213
214 formatAmPm = pattern.contains("a");
215
216 _formatAmPmMap.put(locale, formatAmPm);
217 }
218
219 return formatAmPm;
220 }
221
222 public static Date newDate() {
223 return new Date();
224 }
225
226 public static Date newDate(long date) {
227 return new Date(date);
228 }
229
230 public static long newTime() {
231 Date date = new Date();
232
233 return date.getTime();
234 }
235
236 public static Date parseDate(String dateString, Locale locale)
237 throws ParseException {
238
239 DateFormat dateFormat = DateFormat.getDateInstance(
240 DateFormat.SHORT, locale);
241
242 return dateFormat.parse(dateString);
243 }
244
245 private static final Map<Locale, Boolean> _formatAmPmMap = new HashMap<>();
246
247 }