001: /*
002: * $Id: DateUtils.java,v 1.1 2007/08/15 23:28:24 suricate Exp $
003: *
004: * This code comes from the dormant jdnc project at https://jdnc.dev.java.net/. It is licensed with the LGPL
005: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
006: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
007: */
008: package org.jdesktop.swing.calendar;
009:
010: import java.util.Calendar;
011: import java.util.Date;
012:
013: /**
014: * @author Scott Violet
015: * @version $Revision: 1.1 $
016: */
017: public class DateUtils {
018: private static Calendar CALENDAR = Calendar.getInstance();
019:
020: /**
021: * Returns the last millisecond of the specified date.
022: *
023: * @param date Date to calculate end of day from
024: * @return Last millisecond of <code>date</code>
025: */
026: public static Date endOfDay(Date date) {
027: Calendar calendar = CALENDAR;
028: synchronized (calendar) {
029: calendar.setTime(date);
030: calendar.set(Calendar.HOUR_OF_DAY, 23);
031: calendar.set(Calendar.MILLISECOND, 999);
032: calendar.set(Calendar.SECOND, 59);
033: calendar.set(Calendar.MINUTE, 59);
034: return calendar.getTime();
035: }
036: }
037:
038: /**
039: * Returns a new Date with the hours, milliseconds, seconds and minutes
040: * set to 0.
041: *
042: * @param date Date used in calculating start of day
043: * @return Start of <code>date</code>
044: */
045: public static Date startOfDay(Date date) {
046: Calendar calendar = CALENDAR;
047: synchronized (calendar) {
048: calendar.setTime(date);
049: calendar.set(Calendar.HOUR_OF_DAY, 0);
050: calendar.set(Calendar.MILLISECOND, 0);
051: calendar.set(Calendar.SECOND, 0);
052: calendar.set(Calendar.MINUTE, 0);
053: return calendar.getTime();
054: }
055: }
056:
057: /**
058: * Returns day in millis with the hours, milliseconds, seconds and minutes
059: * set to 0.
060: *
061: * @param date long used in calculating start of day
062: * @return Start of <code>date</code>
063: */
064: public static long startOfDayInMillis(long date) {
065: Calendar calendar = CALENDAR;
066: synchronized (calendar) {
067: calendar.setTimeInMillis(date);
068: calendar.set(Calendar.HOUR_OF_DAY, 0);
069: calendar.set(Calendar.MILLISECOND, 0);
070: calendar.set(Calendar.SECOND, 0);
071: calendar.set(Calendar.MINUTE, 0);
072: return calendar.getTimeInMillis();
073: }
074: }
075:
076: /**
077: * Returns the last millisecond of the specified date.
078: *
079: * @param date long to calculate end of day from
080: * @return Last millisecond of <code>date</code>
081: */
082: public static long endOfDayInMillis(long date) {
083: Calendar calendar = CALENDAR;
084: synchronized (calendar) {
085: calendar.setTimeInMillis(date);
086: calendar.set(Calendar.HOUR_OF_DAY, 23);
087: calendar.set(Calendar.MILLISECOND, 999);
088: calendar.set(Calendar.SECOND, 59);
089: calendar.set(Calendar.MINUTE, 59);
090: return calendar.getTimeInMillis();
091: }
092: }
093:
094: /**
095: * Returns the day after <code>date</code>.
096: *
097: * @param date Date used in calculating next day
098: * @return Day after <code>date</code>.
099: */
100: public static Date nextDay(Date date) {
101: return new Date(addDays(date.getTime(), 1));
102: }
103:
104: /**
105: * Adds <code>amount</code> days to <code>time</code> and returns
106: * the resulting time.
107: *
108: * @param time Base time
109: * @param amount Amount of increment.
110: */
111: public static long addDays(long time, int amount) {
112: Calendar calendar = CALENDAR;
113: synchronized (calendar) {
114: calendar.setTimeInMillis(time);
115: calendar.add(Calendar.DAY_OF_MONTH, amount);
116: return calendar.getTimeInMillis();
117: }
118: }
119:
120: /**
121: * Returns the day after <code>date</code>.
122: *
123: * @param date Date used in calculating next day
124: * @return Day after <code>date</code>.
125: */
126: public static long nextDay(long date) {
127: return addDays(date, 1);
128: }
129:
130: /**
131: * Returns the week after <code>date</code>.
132: *
133: * @param date Date used in calculating next week
134: * @return week after <code>date</code>.
135: */
136: public static long nextWeek(long date) {
137: return addDays(date, 7);
138: }
139:
140: /**
141: * Returns the number of days difference between <code>t1</code> and
142: * <code>t2</code>.
143: *
144: * @param t1 Time 1
145: * @param t2 Time 2
146: * @param checkOverflow indicates whether to check for overflow
147: * @return Number of days between <code>start</code> and <code>end</code>
148: */
149: public static int getDaysDiff(long t1, long t2,
150: boolean checkOverflow) {
151: if (t1 > t2) {
152: long tmp = t1;
153: t1 = t2;
154: t2 = tmp;
155: }
156: Calendar calendar = CALENDAR;
157: synchronized (calendar) {
158: calendar.setTimeInMillis(t1);
159: int delta = 0;
160: while (calendar.getTimeInMillis() < t2) {
161: calendar.add(Calendar.DAY_OF_MONTH, 1);
162: delta++;
163: }
164: if (checkOverflow && (calendar.getTimeInMillis() > t2)) {
165: delta--;
166: }
167: return delta;
168: }
169: }
170:
171: /**
172: * Returns the number of days difference between <code>t1</code> and
173: * <code>t2</code>.
174: *
175: * @param t1 Time 1
176: * @param t2 Time 2
177: * @return Number of days between <code>start</code> and <code>end</code>
178: */
179: public static int getDaysDiff(long t1, long t2) {
180: return getDaysDiff(t1, t2, true);
181: }
182:
183: /*
184: * This methods returns true if the date passed in is the
185: * first day of the year.
186: */
187: public static boolean isFirstOfYear(long date) {
188: boolean ret = false;
189: Calendar calendar = CALENDAR;
190: synchronized (calendar) {
191: calendar.setTimeInMillis(date);
192: int currentYear = calendar.get(Calendar.YEAR);
193: // Check yesterday
194: calendar.add(Calendar.DATE, -1);
195: int yesterdayYear = calendar.get(Calendar.YEAR);
196: ret = (currentYear != yesterdayYear);
197: }
198: return ret;
199: }
200:
201: /*
202: * This methods returns true if the date passed in is the
203: * first day of the month.
204: */
205: public static boolean isFirstOfMonth(long date) {
206: boolean ret = false;
207: Calendar calendar = CALENDAR;
208: synchronized (calendar) {
209: calendar.setTimeInMillis(date);
210: int currentMonth = calendar.get(Calendar.MONTH);
211: // Check yesterday
212: calendar.add(Calendar.DATE, -1);
213: int yesterdayMonth = calendar.get(Calendar.MONTH);
214: ret = (currentMonth != yesterdayMonth);
215: }
216: return ret;
217: }
218:
219: /**
220: * Returns the day before <code>date</code>.
221: *
222: * @param date Date used in calculating previous day
223: * @return Day before <code>date</code>.
224: */
225: public static long previousDay(long date) {
226: return addDays(date, -1);
227: }
228:
229: /**
230: * Returns the week before <code>date</code>.
231: *
232: * @param date Date used in calculating previous week
233: * @return week before <code>date</code>.
234: */
235: public static long previousWeek(long date) {
236: return addDays(date, -7);
237: }
238:
239: /**
240: * Returns the first day before <code>date</code> that has the
241: * day of week matching <code>startOfWeek</code>. For example, if you
242: * want to find the previous monday relative to <code>date</code> you
243: * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
244: *
245: * @param date Base date
246: * @param startOfWeek Calendar constant correspoding to start of week.
247: * @return start of week, return value will have 0 hours, 0 minutes,
248: * 0 seconds and 0 ms.
249: *
250: */
251: public static long getPreviousDay(long date, int startOfWeek) {
252: return getDay(date, startOfWeek, -1);
253: }
254:
255: /**
256: * Returns the first day after <code>date</code> that has the
257: * day of week matching <code>startOfWeek</code>. For example, if you
258: * want to find the next monday relative to <code>date</code> you
259: * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
260: *
261: * @param date Base date
262: * @param startOfWeek Calendar constant correspoding to start of week.
263: * @return start of week, return value will have 0 hours, 0 minutes,
264: * 0 seconds and 0 ms.
265: *
266: */
267: public static long getNextDay(long date, int startOfWeek) {
268: return getDay(date, startOfWeek, 1);
269: }
270:
271: private static long getDay(long date, int startOfWeek, int increment) {
272: Calendar calendar = CALENDAR;
273: synchronized (calendar) {
274: calendar.setTimeInMillis(date);
275: int day = calendar.get(Calendar.DAY_OF_WEEK);
276: // Normalize the view starting date to a week starting day
277: while (day != startOfWeek) {
278: calendar.add(Calendar.DATE, increment);
279: day = calendar.get(Calendar.DAY_OF_WEEK);
280: }
281: return startOfDayInMillis(calendar.getTimeInMillis());
282: }
283: }
284:
285: /**
286: * Returns the previous month.
287: *
288: * @param date Base date
289: * @return previous month
290: */
291: public static long getPreviousMonth(long date) {
292: return incrementMonth(date, -1);
293: }
294:
295: /**
296: * Returns the next month.
297: *
298: * @param date Base date
299: * @return next month
300: */
301: public static long getNextMonth(long date) {
302: return incrementMonth(date, 1);
303: }
304:
305: private static long incrementMonth(long date, int increment) {
306: Calendar calendar = CALENDAR;
307: synchronized (calendar) {
308: calendar.setTimeInMillis(date);
309: calendar.add(Calendar.MONTH, increment);
310: return calendar.getTimeInMillis();
311: }
312: }
313:
314: /**
315: * Returns the date corresponding to the start of the month.
316: *
317: * @param date Base date
318: * @return Start of month.
319: */
320: public static long getStartOfMonth(long date) {
321: return getMonth(date, -1);
322: }
323:
324: /**
325: * Returns the date corresponding to the end of the month.
326: *
327: * @param date Base date
328: * @return End of month.
329: */
330: public static long getEndOfMonth(long date) {
331: return getMonth(date, 1);
332: }
333:
334: private static long getMonth(long date, int increment) {
335: Calendar calendar = CALENDAR;
336: synchronized (calendar) {
337: calendar.setTimeInMillis(date);
338: if (increment == -1) {
339: calendar.set(Calendar.DAY_OF_MONTH, 1);
340: return startOfDayInMillis(calendar.getTimeInMillis());
341: } else {
342: calendar.add(Calendar.MONTH, 1);
343: calendar.set(Calendar.DAY_OF_MONTH, 1);
344: calendar.set(Calendar.HOUR_OF_DAY, 0);
345: calendar.set(Calendar.MILLISECOND, 0);
346: calendar.set(Calendar.SECOND, 0);
347: calendar.set(Calendar.MINUTE, 0);
348: calendar.add(Calendar.MILLISECOND, -1);
349: return calendar.getTimeInMillis();
350: }
351: }
352: }
353:
354: /**
355: * Returns the day of the week.
356: *
357: * @param date date
358: * @return day of week.
359: */
360: public static int getDayOfWeek(long date) {
361: Calendar calendar = CALENDAR;
362: synchronized (calendar) {
363: calendar.setTimeInMillis(date);
364: return (calendar.get(Calendar.DAY_OF_WEEK));
365: }
366: }
367: }
|