1 package org.varienaja.util;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.GregorianCalendar;
8
9
10
11
12
13
14 public class DateTools {
15
16
17
18
19
20
21
22
23 public static Date getDate(Integer year, Integer month, Integer day) {
24 Calendar cal = Calendar.getInstance();
25 cal.clear();
26 cal.set(year,month,day,0,0,0);
27 return cal.getTime();
28 }
29
30
31
32
33
34
35
36
37
38
39
40 public static Date getDate(Integer year, Integer month, Integer day,
41 Integer hour, Integer minute, Integer second) {
42 Calendar cal = Calendar.getInstance();
43 cal.clear();
44 cal.set(year,month,day,hour,minute,second);
45 return cal.getTime();
46 }
47
48
49
50
51
52
53 public static Date yearStart(Integer year) {
54 return DateTools.getDate(year,0,1);
55 }
56
57
58
59
60
61
62 public static Date yearEnd(Integer year) {
63 return DateTools.getDate(year,11,31,23,59,59);
64 }
65
66
67
68
69
70
71
72
73 public static Boolean sameDay(Date d1, Date d2) {
74 GregorianCalendar calOne = new GregorianCalendar();
75 calOne.setTimeInMillis(d1.getTime());
76 GregorianCalendar calTwo = new GregorianCalendar();
77 calTwo.setTimeInMillis(d2.getTime());
78 return (calOne.get(Calendar.YEAR) == calTwo.get(Calendar.YEAR)
79 && calOne.get(Calendar.MONTH) == calTwo.get(Calendar.MONTH)
80 && calOne.get(Calendar.DAY_OF_MONTH) == calTwo.get(Calendar.DAY_OF_MONTH));
81 }
82
83 private static final long ONE_HOUR = 60 * 60 * 1000L;
84
85 public static long daysBetween(Date d1, Date d2) {
86 if(d1==null || d2==null) return Long.MAX_VALUE;
87 return Math.abs( (d2.getTime()-d1.getTime()+ONE_HOUR)/(ONE_HOUR * 24) );
88 }
89
90
91
92
93
94
95 public static Integer getYear(Date d) {
96 Calendar cal = Calendar.getInstance();
97 cal.setTime(d);
98 return cal.get(Calendar.YEAR);
99 }
100
101 static public String formatDate(Date d) {
102 return formatDate(d,"dd-MM-yyyy");
103 }
104
105 static public String formatTimestamp(Date d) {
106 return formatDate(d,"dd-MM-yyyy HH:mm:ss");
107 }
108
109 static public String formatDate(Date d,String pattern) {
110 if (d==null) return "";
111
112 SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
113 df.applyPattern(pattern);
114 return df.format(d);
115 }
116
117
118
119
120
121
122
123 static public Date parseDate(String date,String pattern) {
124 if (date==null) return null;
125
126 SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
127 df.applyPattern(pattern);
128 try {
129 return df.parse(date);
130 } catch (ParseException e) {
131 return null;
132 }
133 }
134
135
136
137
138
139 static public Date currentDate() {
140 GregorianCalendar g=new GregorianCalendar();
141 int Y=g.get(Calendar.YEAR);
142 int M=g.get(Calendar.MONTH)+1;
143 int D=g.get(Calendar.DAY_OF_MONTH);
144
145 return encodeDate(D,M,Y);
146 }
147
148
149
150
151
152 static public Date getFirstOfThisMonth() {
153 GregorianCalendar g=new GregorianCalendar();
154 int Y=g.get(Calendar.YEAR);
155 int M=g.get(Calendar.MONTH)+1;
156
157 return encodeDate(1,M,Y);
158 }
159
160 static public int currentYear() {
161 GregorianCalendar g=new GregorianCalendar();
162 return g.get(Calendar.YEAR);
163 }
164
165
166
167
168
169 static public Date tomorrow() {
170 GregorianCalendar g=new GregorianCalendar();
171 g.add(Calendar.DAY_OF_MONTH, 1);
172 int Y=g.get(Calendar.YEAR);
173 int M=g.get(Calendar.MONTH)+1;
174 int D=g.get(Calendar.DAY_OF_MONTH);
175
176 return encodeDate(D,M,Y);
177 }
178
179 static public Date nullDate() {
180 return encodeDate(1,1,1);
181 }
182
183 static public Date encodeDate(int dd, int mm, int yyyy) {
184 GregorianCalendar g = new GregorianCalendar(yyyy,mm-1,dd);
185 return g.getTime();
186 }
187
188 static public Date encodeDateTime(int dd,int mm, int yyyy, int h, int m, int s) {
189 GregorianCalendar g = new GregorianCalendar(yyyy,mm-1,dd,h,m,s);
190 return g.getTime();
191 }
192
193
194
195
196
197
198 static public Date endOfMonth(Date d) {
199 Calendar now = new GregorianCalendar();
200 if (d!=null) now.setTime(d);
201
202 now.set(Calendar.HOUR_OF_DAY,0);
203 now.set(Calendar.MINUTE,0);
204 now.set(Calendar.SECOND,0);
205 now.set(Calendar.DAY_OF_MONTH,1);
206 now.add(Calendar.MONTH,1);
207 now.add(Calendar.SECOND,-1);
208
209 return now.getTime();
210 }
211
212 static public Date previousMonthEnd(Date d) {
213 if (d==null) return null;
214
215 Calendar prevmonth = new GregorianCalendar();
216 prevmonth.setTime(d);
217 prevmonth.add(Calendar.MONTH,-1);
218
219 return endOfMonth(prevmonth.getTime());
220 }
221
222 static public Date nextMonthEnd(Date d) {
223 if (d==null) return null;
224
225 Calendar nextmonth = new GregorianCalendar();
226 nextmonth.setTime(d);
227 nextmonth.add(Calendar.MONTH,1);
228
229 return endOfMonth(nextmonth.getTime());
230 }
231
232
233
234
235
236 static public Date lastYear() {
237 GregorianCalendar g=new GregorianCalendar();
238 Date lastYear = DateTools.encodeDate(g.get(Calendar.DAY_OF_MONTH),
239 g.get(Calendar.MONTH)+1, g.get(Calendar.YEAR)-1);
240 return lastYear;
241 }
242 }