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 String getCurrentDate(String pattern, Locale locale) {
75 return getDate(new Date(), pattern, locale);
76 }
77
78 public static String getDate(Date date, String pattern, Locale locale) {
79 DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
80
81 return dateFormat.format(date);
82 }
83
84 public static int getDaysBetween(
85 Date startDate, Date endDate, TimeZone timeZone) {
86
87 Calendar startCal = new GregorianCalendar(timeZone);
88
89 startCal.setTime(startDate);
90
91 Calendar endCal = new GregorianCalendar(timeZone);
92
93 endCal.setTime(endDate);
94
95 int daysBetween = 0;
96
97 while (startCal.before(endCal)) {
98 startCal.add(Calendar.DAY_OF_MONTH, 1);
99
100 if (startCal.before(endCal)) {
101 daysBetween++;
102 }
103 }
104
105 return daysBetween;
106 }
107
108 public static DateFormat getISOFormat() {
109 return getISOFormat(StringPool.BLANK);
110 }
111
112 public static DateFormat getISOFormat(String text) {
113 String pattern = StringPool.BLANK;
114
115 if (text.length() == 8) {
116 pattern = "yyyyMMdd";
117 }
118 else if (text.length() == 12) {
119 pattern = "yyyyMMddHHmm";
120 }
121 else if (text.length() == 13) {
122 pattern = "yyyyMMdd'T'HHmm";
123 }
124 else if (text.length() == 14) {
125 pattern = "yyyyMMddHHmmss";
126 }
127 else if (text.length() == 15) {
128 pattern = "yyyyMMdd'T'HHmmss";
129 }
130 else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
131 pattern = "yyyyMMdd'T'HHmmssz";
132 }
133 else {
134 pattern = "yyyyMMddHHmmssz";
135 }
136
137 return new SimpleDateFormat(pattern);
138 }
139
140 public static DateFormat getISO8601Format() {
141 return new SimpleDateFormat(ISO_8601_PATTERN);
142 }
143
144 public static DateFormat getUTCFormat() {
145 return getUTCFormat(StringPool.BLANK);
146 }
147
148 public static DateFormat getUTCFormat(String text) {
149 String pattern = StringPool.BLANK;
150
151 if (text.length() == 8) {
152 pattern = "yyyyMMdd";
153 }
154 else if (text.length() == 12) {
155 pattern = "yyyyMMddHHmm";
156 }
157 else if (text.length() == 13) {
158 pattern = "yyyyMMdd'T'HHmm";
159 }
160 else if (text.length() == 14) {
161 pattern = "yyyyMMddHHmmss";
162 }
163 else if (text.length() == 15) {
164 pattern = "yyyyMMdd'T'HHmmss";
165 }
166 else {
167 pattern = "yyyyMMdd'T'HHmmssz";
168 }
169
170 DateFormat dateFormat = new SimpleDateFormat(pattern);
171
172 dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
173
174 return dateFormat;
175 }
176
177 }