KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > calendar > DateUtils


1 /*
2  * $Id: DateUtils.java,v 1.1 2004/08/12 00:24:33 dmouse Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package org.jdesktop.swing.calendar;
8
9 import java.awt.*;
10 import javax.swing.*;
11 import javax.swing.plaf.*;
12 import java.text.*;
13 import java.util.*;
14
15 /**
16  * @author Scott Violet
17  * @version $Revision: 1.1 $
18  */

19 public class DateUtils {
20     private static Calendar CALENDAR = Calendar.getInstance();
21
22     /**
23      * Returns the last millisecond of the specified date.
24      *
25      * @param date Date to calculate end of day from
26      * @return Last millisecond of <code>date</code>
27      */

28     public static Date endOfDay(Date date) {
29         Calendar calendar = CALENDAR;
30         synchronized(calendar) {
31             calendar.setTime(date);
32             calendar.set(Calendar.HOUR_OF_DAY, 23);
33             calendar.set(Calendar.MILLISECOND, 999);
34             calendar.set(Calendar.SECOND, 59);
35             calendar.set(Calendar.MINUTE, 59);
36             return calendar.getTime();
37         }
38     }
39
40
41     /**
42      * Returns a new Date with the hours, milliseconds, seconds and minutes
43      * set to 0.
44      *
45      * @param date Date used in calculating start of day
46      * @return Start of <code>date</code>
47      */

48     public static Date startOfDay(Date date) {
49         Calendar calendar = CALENDAR;
50         synchronized(calendar) {
51             calendar.setTime(date);
52             calendar.set(Calendar.HOUR_OF_DAY, 0);
53             calendar.set(Calendar.MILLISECOND, 0);
54             calendar.set(Calendar.SECOND, 0);
55             calendar.set(Calendar.MINUTE, 0);
56             return calendar.getTime();
57         }
58     }
59
60      /**
61      * Returns day in millis with the hours, milliseconds, seconds and minutes
62      * set to 0.
63      *
64      * @param date long used in calculating start of day
65      * @return Start of <code>date</code>
66      */

67     public static long startOfDayInMillis(long date) {
68         Calendar calendar = CALENDAR;
69         synchronized(calendar) {
70             calendar.setTimeInMillis(date);
71             calendar.set(Calendar.HOUR_OF_DAY, 0);
72             calendar.set(Calendar.MILLISECOND, 0);
73             calendar.set(Calendar.SECOND, 0);
74             calendar.set(Calendar.MINUTE, 0);
75             return calendar.getTimeInMillis();
76         }
77     }
78
79     /**
80      * Returns the last millisecond of the specified date.
81      *
82      * @param date long to calculate end of day from
83      * @return Last millisecond of <code>date</code>
84      */

85     public static long endOfDayInMillis(long date) {
86         Calendar calendar = CALENDAR;
87         synchronized(calendar) {
88             calendar.setTimeInMillis(date);
89             calendar.set(Calendar.HOUR_OF_DAY, 23);
90             calendar.set(Calendar.MILLISECOND, 999);
91             calendar.set(Calendar.SECOND, 59);
92             calendar.set(Calendar.MINUTE, 59);
93             return calendar.getTimeInMillis();
94         }
95     }
96
97
98     /**
99      * Returns the day after <code>date</code>.
100      *
101      * @param date Date used in calculating next day
102      * @return Day after <code>date</code>.
103      */

104     public static Date nextDay(Date date) {
105         return new Date(addDays(date.getTime(), 1));
106     }
107
108     /**
109      * Adds <code>amount</code> days to <code>time</code> and returns
110      * the resulting time.
111      *
112      * @param time Base time
113      * @param amount Amount of increment.
114      */

115     public static long addDays(long time, int amount) {
116         Calendar calendar = CALENDAR;
117         synchronized(calendar) {
118             calendar.setTimeInMillis(time);
119             calendar.add(Calendar.DAY_OF_MONTH, amount);
120             return calendar.getTimeInMillis();
121         }
122     }
123
124     /**
125      * Returns the day after <code>date</code>.
126      *
127      * @param date Date used in calculating next day
128      * @return Day after <code>date</code>.
129      */

130     public static long nextDay(long date) {
131         return addDays(date, 1);
132     }
133
134     /**
135      * Returns the week after <code>date</code>.
136      *
137      * @param date Date used in calculating next week
138      * @return week after <code>date</code>.
139      */

140     public static long nextWeek(long date) {
141         return addDays(date, 7);
142     }
143
144
145     /**
146      * Returns the number of days difference between <code>t1</code> and
147      * <code>t2</code>.
148      *
149      * @param t1 Time 1
150      * @param t2 Time 2
151      * @param checkOverflow indicates whether to check for overflow
152      * @return Number of days between <code>start</code> and <code>end</code>
153      */

154     public static int getDaysDiff(long t1, long t2, boolean checkOverflow) {
155         if (t1 > t2) {
156             long tmp = t1;
157             t1 = t2;
158             t2 = tmp;
159         }
160         Calendar calendar = CALENDAR;
161         synchronized(calendar) {
162             calendar.setTimeInMillis(t1);
163             int delta = 0;
164             while (calendar.getTimeInMillis() < t2) {
165                 calendar.add(Calendar.DAY_OF_MONTH, 1);
166                 delta++;
167             }
168             if (checkOverflow && (calendar.getTimeInMillis() > t2)) {
169                 delta--;
170             }
171             return delta;
172         }
173     }
174
175    /**
176      * Returns the number of days difference between <code>t1</code> and
177      * <code>t2</code>.
178      *
179      * @param t1 Time 1
180      * @param t2 Time 2
181      * @return Number of days between <code>start</code> and <code>end</code>
182      */

183       public static int getDaysDiff(long t1, long t2) {
184        return getDaysDiff(t1, t2, true);
185     }
186
187     /*
188      * This methods returns true if the date passed in is the
189      * first day of the year.
190      */

191     public static boolean isFirstOfYear(long date) {
192         boolean ret = false;
193         Calendar calendar = CALENDAR;
194         synchronized(calendar) {
195             calendar.setTimeInMillis(date);
196             int currentYear = calendar.get(Calendar.YEAR);
197             // Check yesterday
198
calendar.add(Calendar.DATE,-1);
199             int yesterdayYear = calendar.get(Calendar.YEAR);
200             ret = (currentYear != yesterdayYear);
201         }
202         return ret;
203     }
204
205     /*
206      * This methods returns true if the date passed in is the
207      * first day of the month.
208      */

209     public static boolean isFirstOfMonth(long date) {
210         boolean ret = false;
211         Calendar calendar = CALENDAR;
212         synchronized(calendar) {
213             calendar.setTimeInMillis(date);
214             int currentMonth = calendar.get(Calendar.MONTH);
215             // Check yesterday
216
calendar.add(Calendar.DATE,-1);
217             int yesterdayMonth = calendar.get(Calendar.MONTH);
218             ret = (currentMonth != yesterdayMonth);
219         }
220         return ret;
221     }
222
223
224     /**
225      * Returns the day before <code>date</code>.
226      *
227      * @param date Date used in calculating previous day
228      * @return Day before <code>date</code>.
229      */

230     public static long previousDay(long date) {
231         return addDays(date, -1);
232     }
233
234     /**
235      * Returns the week before <code>date</code>.
236      *
237      * @param date Date used in calculating previous week
238      * @return week before <code>date</code>.
239      */

240     public static long previousWeek(long date) {
241         return addDays(date, -7);
242     }
243
244
245     /**
246      * Returns the first day before <code>date</code> that has the
247      * day of week matching <code>startOfWeek</code>. For example, if you
248      * want to find the previous monday relative to <code>date</code> you
249      * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
250      *
251      * @param date Base date
252      * @param startOfWeek Calendar constant correspoding to start of week.
253      * @return start of week, return value will have 0 hours, 0 minutes,
254      * 0 seconds and 0 ms.
255      *
256      */

257     public static long getPreviousDay(long date, int startOfWeek) {
258         return getDay(date, startOfWeek, -1);
259     }
260
261     /**
262      * Returns the first day after <code>date</code> that has the
263      * day of week matching <code>startOfWeek</code>. For example, if you
264      * want to find the next monday relative to <code>date</code> you
265      * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
266      *
267      * @param date Base date
268      * @param startOfWeek Calendar constant correspoding to start of week.
269      * @return start of week, return value will have 0 hours, 0 minutes,
270      * 0 seconds and 0 ms.
271      *
272      */

273     public static long getNextDay(long date, int startOfWeek) {
274         return getDay(date, startOfWeek, 1);
275     }
276
277     private static long getDay(long date, int startOfWeek, int increment) {
278         Calendar calendar = CALENDAR;
279         synchronized(calendar) {
280             calendar.setTimeInMillis(date);
281             int day = calendar.get(Calendar.DAY_OF_WEEK);
282             // Normalize the view starting date to a week starting day
283
while (day != startOfWeek) {
284                 calendar.add(Calendar.DATE, increment);
285                 day = calendar.get(Calendar.DAY_OF_WEEK);
286             }
287             return startOfDayInMillis(calendar.getTimeInMillis());
288         }
289     }
290
291     /**
292      * Returns the previous month.
293      *
294      * @param date Base date
295      * @return previous month
296      */

297     public static long getPreviousMonth(long date) {
298         return incrementMonth(date, -1);
299     }
300
301     /**
302      * Returns the next month.
303      *
304      * @param date Base date
305      * @return next month
306      */

307     public static long getNextMonth(long date) {
308         return incrementMonth(date, 1);
309     }
310
311     private static long incrementMonth(long date, int increment) {
312         Calendar calendar = CALENDAR;
313         synchronized(calendar) {
314             calendar.setTimeInMillis(date);
315             calendar.add(Calendar.MONTH, increment);
316             return calendar.getTimeInMillis();
317         }
318     }
319
320     /**
321      * Returns the date corresponding to the start of the month.
322      *
323      * @param date Base date
324      * @return Start of month.
325      */

326     public static long getStartOfMonth(long date) {
327         return getMonth(date, -1);
328     }
329
330     /**
331      * Returns the date corresponding to the end of the month.
332      *
333      * @param date Base date
334      * @return End of month.
335      */

336     public static long getEndOfMonth(long date) {
337         return getMonth(date, 1);
338     }
339
340     private static long getMonth(long date, int increment) {
341         Calendar calendar = CALENDAR;
342         synchronized(calendar) {
343             calendar.setTimeInMillis(date);
344             if (increment == -1) {
345                 calendar.set(Calendar.DAY_OF_MONTH, 1);
346                 return startOfDayInMillis(calendar.getTimeInMillis());
347             }
348             else {
349                 calendar.add(Calendar.MONTH, 1);
350                 calendar.set(Calendar.DAY_OF_MONTH, 1);
351                 calendar.set(Calendar.HOUR_OF_DAY, 0);
352                 calendar.set(Calendar.MILLISECOND, 0);
353                 calendar.set(Calendar.SECOND, 0);
354                 calendar.set(Calendar.MINUTE, 0);
355                 calendar.add(Calendar.MILLISECOND, -1);
356                 return calendar.getTimeInMillis();
357             }
358         }
359     }
360
361     /**
362      * Returns the day of the week.
363      *
364      * @param date date
365      * @return day of week.
366      */

367     public static int getDayOfWeek(long date) {
368        Calendar calendar = CALENDAR;
369         synchronized(calendar) {
370             calendar.setTimeInMillis(date);
371             return (calendar.get(Calendar.DAY_OF_WEEK));
372         }
373     }
374 }
375
Popular Tags