001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.text.DateFormat;
018 import java.text.Format;
019
020 import java.util.Calendar;
021 import java.util.Date;
022 import java.util.GregorianCalendar;
023 import java.util.Locale;
024 import java.util.TimeZone;
025
026
029 public class DateUtil {
030
031 public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
032
033 public static int compareTo(Date date1, Date date2) {
034 return compareTo(date1, date2, false);
035 }
036
037 public static int compareTo(
038 Date date1, Date date2, boolean ignoreMilliseconds) {
039
040
041
042
043
044 if ((date1 != null) && (date2 == null)) {
045 return -1;
046 }
047 else if ((date1 == null) && (date2 != null)) {
048 return 1;
049 }
050 else if ((date1 == null) && (date2 == null)) {
051 return 0;
052 }
053
054 long time1 = date1.getTime();
055 long time2 = date2.getTime();
056
057 if (ignoreMilliseconds) {
058 time1 = time1 / Time.SECOND;
059 time2 = time2 / Time.SECOND;
060 }
061
062 if (time1 == time2) {
063 return 0;
064 }
065 else if (time1 < time2) {
066 return -1;
067 }
068 else {
069 return 1;
070 }
071 }
072
073 public static boolean equals(Date date1, Date date2) {
074 if (compareTo(date1, date2) == 0) {
075 return true;
076 }
077 else {
078 return false;
079 }
080 }
081
082 public static boolean equals(
083 Date date1, Date date2, boolean ignoreMilliseconds) {
084
085 if (!ignoreMilliseconds) {
086 return equals(date1, date2);
087 }
088
089 long deltaTime = date1.getTime() - date2.getTime();
090
091 if ((deltaTime > -1000) && (deltaTime < 1000)) {
092 return true;
093 }
094 else {
095 return false;
096 }
097 }
098
099 public static String getCurrentDate(String pattern, Locale locale) {
100 return getDate(new Date(), pattern, locale);
101 }
102
103 public static String getCurrentDate(
104 String pattern, Locale locale, TimeZone timeZone) {
105
106 return getDate(new Date(), pattern, locale, timeZone);
107 }
108
109 public static String getDate(Date date, String pattern, Locale locale) {
110 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
111 pattern, locale);
112
113 return dateFormat.format(date);
114 }
115
116 public static String getDate(
117 Date date, String pattern, Locale locale, TimeZone timeZone) {
118
119 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
120 pattern, locale, timeZone);
121
122 return dateFormat.format(date);
123 }
124
125 public static int getDaysBetween(
126 Date startDate, Date endDate, TimeZone timeZone) {
127
128 int offset = timeZone.getRawOffset();
129
130 Calendar startCal = new GregorianCalendar(timeZone);
131
132 startCal.setTime(startDate);
133 startCal.add(Calendar.MILLISECOND, offset);
134
135 Calendar endCal = new GregorianCalendar(timeZone);
136
137 endCal.setTime(endDate);
138 endCal.add(Calendar.MILLISECOND, offset);
139
140 int daysBetween = 0;
141
142 while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
143 startCal.add(Calendar.DAY_OF_MONTH, 1);
144
145 daysBetween++;
146 }
147
148 return daysBetween;
149 }
150
151 public static DateFormat getISOFormat() {
152 return getISOFormat(StringPool.BLANK);
153 }
154
155 public static DateFormat getISOFormat(String text) {
156 String pattern = StringPool.BLANK;
157
158 if (text.length() == 8) {
159 pattern = "yyyyMMdd";
160 }
161 else if (text.length() == 12) {
162 pattern = "yyyyMMddHHmm";
163 }
164 else if (text.length() == 13) {
165 pattern = "yyyyMMdd'T'HHmm";
166 }
167 else if (text.length() == 14) {
168 pattern = "yyyyMMddHHmmss";
169 }
170 else if (text.length() == 15) {
171 pattern = "yyyyMMdd'T'HHmmss";
172 }
173 else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
174 pattern = "yyyyMMdd'T'HHmmssz";
175 }
176 else {
177 pattern = "yyyyMMddHHmmssz";
178 }
179
180 return DateFormatFactoryUtil.getSimpleDateFormat(pattern);
181 }
182
183 public static DateFormat getISO8601Format() {
184 return DateFormatFactoryUtil.getSimpleDateFormat(ISO_8601_PATTERN);
185 }
186
187 public static DateFormat getUTCFormat() {
188 return getUTCFormat(StringPool.BLANK);
189 }
190
191 public static DateFormat getUTCFormat(String text) {
192 String pattern = StringPool.BLANK;
193
194 if (text.length() == 8) {
195 pattern = "yyyyMMdd";
196 }
197 else if (text.length() == 12) {
198 pattern = "yyyyMMddHHmm";
199 }
200 else if (text.length() == 13) {
201 pattern = "yyyyMMdd'T'HHmm";
202 }
203 else if (text.length() == 14) {
204 pattern = "yyyyMMddHHmmss";
205 }
206 else if (text.length() == 15) {
207 pattern = "yyyyMMdd'T'HHmmss";
208 }
209 else {
210 pattern = "yyyyMMdd'T'HHmmssz";
211 }
212
213 return DateFormatFactoryUtil.getSimpleDateFormat(
214 pattern, TimeZoneUtil.getTimeZone(StringPool.UTC));
215 }
216
217 public static Date newDate() {
218 return new Date();
219 }
220
221 public static Date newDate(long date) {
222 return new Date(date);
223 }
224
225 public static long newTime() {
226 Date date = new Date();
227
228 return date.getTime();
229 }
230
231 }