View Javadoc

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   * Date/Time tools.
11   * @author drexler
12   * @version $Id: DateTools.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
13   */
14  public class DateTools {
15  
16  	/**
17  	 * Retourneert een specifieke datum om middernacht.
18  	 * @param year Het gewenste jaar
19  	 * @param month De gewenste maand (januari is 0, december=11)
20  	 * @param day De gewenste dag (1=1, 31=31)
21  	 * @return Een Date object met de gewenste datum om middernacht.
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  	 * Retourneert een specifieke datum en tijd.
32  	 * @param year Het gewenste jaar
33  	 * @param month De gewenste maand (januari is 0, december=11)
34  	 * @param day De gewenste dag (1=1, 31=31)
35  	 * @param hour Het gewenste uur
36  	 * @param minute De gewenste minuut
37  	 * @param second De gewenste seconde.
38  	 * @return Een Date object met de gewenste datum om middernacht.
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  	 * Retourneert de eerste seconde van het gespecificeerde jaar.
50  	 * @param year Het gewenste jaar.
51  	 * @return Datum/tijd van de eerste seconde in het gewenste jaar.
52  	 */
53  	public static Date yearStart(Integer year) {
54  		return DateTools.getDate(year,0,1);
55  	}
56  	
57  	/**
58  	 * Retourneert de laatste seconde van het gespecificeerde jaar.
59  	 * @param year Het gewenste jaar.
60  	 * @return Datum/tijd van de eerste seconde in het gewenste jaar.
61  	 */
62  	public static Date yearEnd(Integer year) {
63  		return DateTools.getDate(year,11,31,23,59,59);
64  	}
65  
66  	/**
67  	 * Retourneert true als de 2 Date objecten in dezelfde dag vallen
68  	 * en anders false.
69  	 * @param d1 Datum 1
70  	 * @param d2 Datum 2
71  	 * @return True als beide tijden in dezelfde dag vallen, anders false.
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  	 * Retournert het jaar waarin de datum valt.
92  	 * @param d De datum
93  	 * @return Het jaar waarin de gespecificeerde datum valt.
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 	 * Tries to convert a String into a Date object.
119 	 * @param date The string representation.
120 	 * @param pattern The pattern to use. (e.g. dd-MM-yyyy)
121 	 * @return The Date, or null if the input could not be converted.
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 	 * Levert de datum van vandaag.
137 	 * @return De datum van vandaag.
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 	 * Returns a Date object consisting of 1-MM-YYYY
150 	 * @return A Date
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 	 * Levert de datum van morgen.
167 	 * @return De datum van morgen.
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 	 * @param d The month of which to return the last moment of.
195 	 * @return returns the timestamp of the last second of the month given. If 
196 	 * the parameter was null, the current month is used
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 	 * returns the date exactly 1 year ago (not including time of the day).
234 	 * @return The date 1 year ago.
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 }