001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Gereon Fassbender, Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.components.util;
014:
015: import java.util.Calendar;
016: import java.util.Date;
017: import java.util.TimeZone;
018:
019: /** Tools for manipulating dates.
020: * At the moment of writing rapla internaly stores all appointments
021: * in the GMT timezone.
022: */
023: public abstract class DateTools {
024: public static final int DAYS_PER_WEEK = 7;
025: public static final long MILLISECONDS_PER_MINUTE = 1000 * 60;
026: public static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * 60;
027: public static final long MILLISECONDS_PER_DAY = 24 * MILLISECONDS_PER_HOUR;
028: public static final long MILLISECONDS_PER_WEEK = 7 * MILLISECONDS_PER_DAY;
029: public static TimeZone GMT = TimeZone.getTimeZone("GMT+0");
030:
031: public static int getHourOfDay(long date) {
032: return (int) ((date % MILLISECONDS_PER_DAY) / MILLISECONDS_PER_HOUR);
033: }
034:
035: public static int getMinuteOfHour(long date) {
036: return (int) ((date % MILLISECONDS_PER_HOUR) / MILLISECONDS_PER_MINUTE);
037: }
038:
039: /** sets time of day to 0:00.
040: @see #cutDate(Date)
041: */
042: public static long cutDate(long date) {
043: return (date - (date % MILLISECONDS_PER_DAY));
044: }
045:
046: public static boolean isMidnight(long date) {
047: return cutDate(date) == date;
048: }
049:
050: public static boolean isMidnight(Date date) {
051: return isMidnight(date.getTime());
052: }
053:
054: /** sets time of day to 0:00.
055: @see #cutDate(Date)
056: */
057: public static void cutDate(Calendar calendar) {
058: calendar.set(Calendar.HOUR_OF_DAY, 0);
059: calendar.set(Calendar.MINUTE, 0);
060: calendar.set(Calendar.SECOND, 0);
061: calendar.set(Calendar.MILLISECOND, 0);
062: }
063:
064: /** sets time of day to 0:00. */
065: public static Date cutDate(Date date) {
066: return new Date(cutDate(date.getTime()));
067: }
068:
069: static TimeZone timeZone = TimeZone.getTimeZone("GMT");
070:
071: /** same as TimeZone.getTimeZone("GMT"). */
072: public static TimeZone getTimeZone() {
073: return timeZone;
074: }
075:
076: /** sets time of day to 0:00 and increases day.
077: @see #fillDate(Date)
078: */
079: public static long fillDate(long date) {
080: // cut date
081: long cuttedDate = (date - (date % MILLISECONDS_PER_DAY));
082: return cuttedDate + MILLISECONDS_PER_DAY;
083: }
084:
085: public static Date fillDate(Date date) {
086: return new Date(fillDate(date.getTime()));
087: }
088:
089: /** Monday 24:00 = tuesday 0:00.
090: But the first means end of monday and the second start of tuesday.
091: The default DateFormat always displays tuesday.
092: If you want to edit the first interpretation in calendar components.
093: call addDay() to add 1 day to the given date before displaying
094: and subDay() for mapping a day back after editing.
095: @see #subDay
096: @see #addDays
097: */
098: public static Date addDay(Date date) {
099: return new Date(date.getTime() + MILLISECONDS_PER_DAY);
100: }
101:
102: /** see #addDay*/
103: public static Date addDays(Date date, int days) {
104: return new Date(date.getTime() + MILLISECONDS_PER_DAY * days);
105: }
106:
107: /**
108: @see #addDay
109: @see #subDays
110: */
111: public static Date subDay(Date date) {
112: return new Date(date.getTime() - MILLISECONDS_PER_DAY);
113: }
114:
115: /**
116: @see #addDay
117: */
118: public static Date subDays(Date date, int days) {
119: return new Date(date.getTime() - MILLISECONDS_PER_DAY * days);
120: }
121:
122: /** returns if the two dates are one the same date.
123: * Dates must be in GMT */
124: static public boolean isSameDay(long d1, long d2) {
125: return cutDate(d1) == cutDate(d2);
126: }
127:
128: /** uses the calendar-object for date comparison.
129: * Use this for non GMT Dates*/
130: static public boolean isSameDay(Calendar calendar, Date d1, Date d2) {
131: calendar.setTime(d1);
132: int era1 = calendar.get(Calendar.ERA);
133: int year1 = calendar.get(Calendar.YEAR);
134: int day_of_year1 = calendar.get(Calendar.DAY_OF_YEAR);
135: calendar.setTime(d2);
136: int era2 = calendar.get(Calendar.ERA);
137: int year2 = calendar.get(Calendar.YEAR);
138: int day_of_year2 = calendar.get(Calendar.DAY_OF_YEAR);
139: return (era1 == era2 && year1 == year2 && day_of_year1 == day_of_year2);
140: }
141:
142: static public long countDays(Date start, Date end) {
143: return (cutDate(end.getTime()) - cutDate(start.getTime()))
144: / MILLISECONDS_PER_DAY;
145: }
146:
147: static public Calendar createGMTCalendar() {
148: return Calendar.getInstance(GMT);
149: }
150:
151: }
|