1 /***
2 * Calendar Tag Library
3 * Copyright (C) 2005 James Smith
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should be able to download a copy of the LGPL from
19 * www.opensource.org/licenses/lgpl-license.php
20 */
21 package org.calendartag.util;
22
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.GregorianCalendar;
26
27 /***
28 * @author James Smith
29 * @since Oct 3, 2004
30 */
31 public class CalendarTagUtil {
32
33 /***
34 * Removes the milliseconds, minutes, seconds, and hours from a <code>Calendar</code> object
35 * @param calendar the <code>Calendar</code> object
36 */
37 public static void trimCalendar(Calendar calendar) {
38 calendar.set(Calendar.MILLISECOND, 0);
39 calendar.set(Calendar.MINUTE, 0);
40 calendar.set(Calendar.SECOND, 0);
41 calendar.set(Calendar.HOUR_OF_DAY, 0);
42 }
43
44 /***
45 * Uses <code>trimCalendar</code> to remove milliseconds, minutes, seconds, and hours from a <code>Date</code> object
46 * @param date The <code>Date</code> object
47 */
48 public static void trimDate(Date date) {
49 Calendar calendar = new GregorianCalendar();
50 calendar.setTime(date);
51 trimCalendar(calendar);
52 date = calendar.getTime();
53 }
54
55 /***
56 * Determines if two <code>Calendar</code> objects are the same day, month and year.
57 * @param a the first <code>Calendar</code> object
58 * @param b the second <code>Calendar</code> object
59 * @return true if they are the same day, month and year. otherwise false
60 */
61 public static boolean isSameDay(Calendar a, Calendar b) {
62 return (a.get(Calendar.DATE) == b.get(Calendar.DATE) &&
63 a.get(Calendar.MONTH) == b.get(Calendar.MONTH) &&
64 a.get(Calendar.YEAR) == b.get(Calendar.YEAR));
65 }
66
67 /***
68 * Determines if the leftCalendar is before, during or after the rightCalendar based on the year, month, and day of
69 * the date only. This is less specific than <code>Date.before(Date)</code> since it only compares based on
70 * the day and is capable of returning 0 when the day is the same
71 * @param leftCalendar the left calendar
72 * @param rightCalendar the right calendar
73 * @return -1 if the left calendar is before the right calendar, 0 if they are on the same day, +1 if the left
74 * calendar is after the right calendar
75 */
76 public static int dayCompare(Calendar leftCalendar, Calendar rightCalendar) {
77
78 if (leftCalendar.get(Calendar.DATE) == rightCalendar.get(Calendar.DATE) &&
79 leftCalendar.get(Calendar.MONTH) == rightCalendar.get(Calendar.MONTH) &&
80 leftCalendar.get(Calendar.YEAR) == rightCalendar.get(Calendar.YEAR)) {
81 return 0;
82 } else if (leftCalendar.before(rightCalendar)) {
83 return -1;
84 } else {
85 return 1;
86 }
87
88 }
89
90 /***
91 * Accurately calculates the difference between two calendars in days.
92 * @param a the first calendar
93 * @param b the second calendar
94 * @return the number of days difference, 0 if they are the same,
95 */
96 public static int differenceInDays(Calendar a, Calendar b) {
97
98 Calendar calendar = new GregorianCalendar();
99 calendar = (Calendar) b.clone();
100
101 int dayCompared = dayCompare(a, calendar);
102 int dayDifference = 0;
103 while (dayCompared != 0) {
104 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + dayCompared);
105 dayCompared = dayCompare(a, calendar);
106 dayDifference++;
107 }
108
109 return dayDifference;
110
111 }
112
113 }