michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.util; michael@0: michael@0: import java.text.MessageFormat; michael@0: import java.util.ArrayList; michael@0: import java.util.Calendar; michael@0: import java.util.List; michael@0: import java.util.TimeZone; michael@0: michael@0: import net.fortuna.ical4j.model.Date; michael@0: import net.fortuna.ical4j.model.DateTime; michael@0: import net.fortuna.ical4j.model.parameter.Value; michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created on 26/06/2005 michael@0: * michael@0: * Implements a collection of utility methods relevant to date processing. michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public final class Dates { michael@0: michael@0: /** michael@0: * Number of milliseconds in one second. michael@0: */ michael@0: public static final long MILLIS_PER_SECOND = 1000; michael@0: michael@0: /** michael@0: * Number of milliseconds in one minute. michael@0: */ michael@0: public static final long MILLIS_PER_MINUTE = 60000; michael@0: michael@0: /** michael@0: * Number of milliseconds in one hour. michael@0: */ michael@0: public static final long MILLIS_PER_HOUR = 3600000; michael@0: michael@0: /** michael@0: * Number of milliseconds in one day. michael@0: */ michael@0: public static final long MILLIS_PER_DAY = 86400000; michael@0: michael@0: /** michael@0: * Number of milliseconds in one week. michael@0: */ michael@0: public static final long MILLIS_PER_WEEK = 604800000; michael@0: michael@0: /** michael@0: * Number of days in one week. michael@0: */ michael@0: public static final int DAYS_PER_WEEK = 7; michael@0: michael@0: /** michael@0: * Constant indicating precision to the second. michael@0: */ michael@0: public static final int PRECISION_SECOND = 0; michael@0: michael@0: /** michael@0: * Constant indicating precision to the day. michael@0: */ michael@0: public static final int PRECISION_DAY = 1; michael@0: michael@0: /** michael@0: * Maximum number of weeks per year. michael@0: */ michael@0: public static final int MAX_WEEKS_PER_YEAR = 53; michael@0: michael@0: /** michael@0: * Maximum number of days per year. michael@0: */ michael@0: public static final int MAX_DAYS_PER_YEAR = 366; michael@0: michael@0: /** michael@0: * Maximum number of days per month. michael@0: */ michael@0: public static final int MAX_DAYS_PER_MONTH = 31; michael@0: michael@0: private static final String INVALID_WEEK_MESSAGE = "Invalid week number [{0}]"; michael@0: michael@0: private static final String INVALID_YEAR_DAY_MESSAGE = "Invalid year day [{0}]"; michael@0: michael@0: private static final String INVALID_MONTH_DAY_MESSAGE = "Invalid month day [{0}]"; michael@0: michael@0: /** michael@0: * Constructor made private to prevent instantiation. michael@0: */ michael@0: private Dates() { michael@0: } michael@0: michael@0: /** michael@0: * Returns the absolute week number for the year specified by the michael@0: * supplied date. Note that a value of zero (0) is invalid for the michael@0: * weekNo parameter and an IllegalArgumentException michael@0: * will be thrown. michael@0: * @param date a date instance representing a week of the year michael@0: * @param weekNo a week number offset michael@0: * @return the absolute week of the year for the specified offset michael@0: */ michael@0: public static int getAbsWeekNo(final java.util.Date date, final int weekNo) { michael@0: if (weekNo == 0 || weekNo < -MAX_WEEKS_PER_YEAR || weekNo > MAX_WEEKS_PER_YEAR) { michael@0: throw new IllegalArgumentException(MessageFormat.format(INVALID_WEEK_MESSAGE, michael@0: new Object[] {new Integer(weekNo)})); michael@0: } michael@0: if (weekNo > 0) { michael@0: return weekNo; michael@0: } michael@0: final Calendar cal = Calendar.getInstance(); michael@0: cal.setTime(date); michael@0: final int year = cal.get(Calendar.YEAR); michael@0: // construct a list of possible week numbers.. michael@0: final List weeks = new ArrayList(); michael@0: cal.set(Calendar.WEEK_OF_YEAR, 1); michael@0: while (cal.get(Calendar.YEAR) == year) { michael@0: weeks.add(new Integer(cal.get(Calendar.WEEK_OF_YEAR))); michael@0: cal.add(Calendar.WEEK_OF_YEAR, 1); michael@0: } michael@0: return ((Integer) weeks.get(weeks.size() + weekNo)).intValue(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the absolute year day for the year specified by the michael@0: * supplied date. Note that a value of zero (0) is invalid for the michael@0: * yearDay parameter and an IllegalArgumentException michael@0: * will be thrown. michael@0: * @param date a date instance representing a day of the year michael@0: * @param yearDay a day of year offset michael@0: * @return the absolute day of month for the specified offset michael@0: */ michael@0: public static int getAbsYearDay(final java.util.Date date, final int yearDay) { michael@0: if (yearDay == 0 || yearDay < -MAX_DAYS_PER_YEAR || yearDay > MAX_DAYS_PER_YEAR) { michael@0: throw new IllegalArgumentException(MessageFormat.format(INVALID_YEAR_DAY_MESSAGE, michael@0: new Object[] {new Integer(yearDay)})); michael@0: } michael@0: if (yearDay > 0) { michael@0: return yearDay; michael@0: } michael@0: final Calendar cal = Calendar.getInstance(); michael@0: cal.setTime(date); michael@0: final int year = cal.get(Calendar.YEAR); michael@0: // construct a list of possible year days.. michael@0: final List days = new ArrayList(); michael@0: cal.set(Calendar.DAY_OF_YEAR, 1); michael@0: while (cal.get(Calendar.YEAR) == year) { michael@0: days.add(new Integer(cal.get(Calendar.DAY_OF_YEAR))); michael@0: cal.add(Calendar.DAY_OF_YEAR, 1); michael@0: } michael@0: return ((Integer) days.get(days.size() + yearDay)).intValue(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the absolute month day for the month specified by the michael@0: * supplied date. Note that a value of zero (0) is invalid for the michael@0: * monthDay parameter and an IllegalArgumentException michael@0: * will be thrown. michael@0: * @param date a date instance representing a day of the month michael@0: * @param monthDay a day of month offset michael@0: * @return the absolute day of month for the specified offset michael@0: */ michael@0: public static int getAbsMonthDay(final java.util.Date date, final int monthDay) { michael@0: if (monthDay == 0 || monthDay < -MAX_DAYS_PER_MONTH || monthDay > MAX_DAYS_PER_MONTH) { michael@0: throw new IllegalArgumentException(MessageFormat.format(INVALID_MONTH_DAY_MESSAGE, michael@0: new Object[] {new Integer(monthDay)})); michael@0: } michael@0: if (monthDay > 0) { michael@0: return monthDay; michael@0: } michael@0: final Calendar cal = Calendar.getInstance(); michael@0: cal.setTime(date); michael@0: final int month = cal.get(Calendar.MONTH); michael@0: // construct a list of possible month days.. michael@0: final List days = new ArrayList(); michael@0: cal.set(Calendar.DAY_OF_MONTH, 1); michael@0: while (cal.get(Calendar.MONTH) == month) { michael@0: days.add(new Integer(cal.get(Calendar.DAY_OF_MONTH))); michael@0: cal.add(Calendar.DAY_OF_MONTH, 1); michael@0: } michael@0: return ((Integer) days.get(days.size() + monthDay)).intValue(); michael@0: } michael@0: michael@0: /** michael@0: * Returns a new date instance of the specified type. If no type is michael@0: * specified a DateTime instance is returned. michael@0: * @param date a seed Java date instance michael@0: * @param type the type of date instance michael@0: * @return an instance of net.fortuna.ical4j.model.Date michael@0: */ michael@0: public static Date getInstance(final java.util.Date date, final Value type) { michael@0: if (Value.DATE.equals(type)) { michael@0: return new Date(date); michael@0: } michael@0: return new DateTime(date); michael@0: } michael@0: michael@0: /** michael@0: * Returns an instance of java.util.Calendar that is suitably michael@0: * initialised for working with the specified date. michael@0: * @param date a date instance michael@0: * @return a java.util.Calendar michael@0: */ michael@0: public static Calendar getCalendarInstance(final Date date) { michael@0: Calendar instance = null; michael@0: if (date instanceof DateTime) { michael@0: final DateTime dateTime = (DateTime) date; michael@0: if (dateTime.getTimeZone() != null) { michael@0: instance = Calendar.getInstance(dateTime.getTimeZone()); michael@0: } michael@0: else if (dateTime.isUtc()) { michael@0: instance = Calendar.getInstance(TimeZones.getUtcTimeZone()); michael@0: } michael@0: else { michael@0: // a date-time without a timezone but not UTC is floating michael@0: instance = Calendar.getInstance(); michael@0: } michael@0: } michael@0: else { michael@0: instance = Calendar.getInstance(TimeZones.getDateTimeZone()); michael@0: } michael@0: return instance; michael@0: } michael@0: michael@0: /** michael@0: * @param time the time value to round michael@0: * @param precision the rounding precision michael@0: * @return a round time value michael@0: * @deprecated It is not all that useful to perform rounding without specifying an michael@0: * explicit timezone. michael@0: */ michael@0: public static long round(final long time, final int precision) { michael@0: return round(time, precision, TimeZone.getDefault()); michael@0: // return round(time, precision, TimeZone.getTimeZone(TimeZones.UTC_ID)); michael@0: /* michael@0: long newTime = time; michael@0: if (precision == PRECISION_DAY) { michael@0: long remainder = newTime%(1000*60*60); // get the mod remainder using milliseconds*seconds*min michael@0: newTime = newTime-remainder; michael@0: // remove the remainder from the time to clear the milliseconds, seconds and minutes michael@0: } michael@0: else if (precision == PRECISION_SECOND) { michael@0: long remainder = newTime%(1000); // get the mod remainder using milliseconds michael@0: newTime = newTime-remainder; // remove the remainder from the time to clear the milliseconds michael@0: } michael@0: return newTime; michael@0: */ michael@0: } michael@0: michael@0: /** michael@0: * Rounds a time value to remove any precision smaller than specified. michael@0: * @param time the time value to round michael@0: * @param precision the rounding precision michael@0: * @param tz the timezone of the rounded value michael@0: * @return a round time value michael@0: */ michael@0: public static long round(final long time, final int precision, final TimeZone tz) { michael@0: if ((precision == PRECISION_SECOND) && ((time % Dates.MILLIS_PER_SECOND) == 0)) { michael@0: return time; michael@0: } michael@0: final Calendar cal = Calendar.getInstance(tz); michael@0: cal.setTimeInMillis(time); michael@0: if (precision == PRECISION_DAY) { michael@0: // return (long) Math.floor(time / (double) Dates.MILLIS_PER_DAY) * Dates.MILLIS_PER_DAY; michael@0: cal.set(Calendar.HOUR_OF_DAY, 0); michael@0: cal.clear(Calendar.MINUTE); michael@0: cal.clear(Calendar.SECOND); michael@0: cal.clear(Calendar.MILLISECOND); michael@0: } michael@0: else if (precision == PRECISION_SECOND) { michael@0: // return (long) Math.floor(time / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND; michael@0: cal.clear(Calendar.MILLISECOND); michael@0: } michael@0: // unrecognised precision.. michael@0: return cal.getTimeInMillis(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the {@code System.currentTimeMillis()}, rounded to the second. michael@0: *

By doing a rough rounding here, we avoid an expensive java.util.Calendar based michael@0: * rounding later on.

michael@0: * @return the current time in millisec. michael@0: */ michael@0: public static long getCurrentTimeRounded() { michael@0: return (long) Math.floor(System.currentTimeMillis() / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND; michael@0: } michael@0: }