1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/util/Dates.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,315 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.util; 1.36 + 1.37 +import java.text.MessageFormat; 1.38 +import java.util.ArrayList; 1.39 +import java.util.Calendar; 1.40 +import java.util.List; 1.41 +import java.util.TimeZone; 1.42 + 1.43 +import net.fortuna.ical4j.model.Date; 1.44 +import net.fortuna.ical4j.model.DateTime; 1.45 +import net.fortuna.ical4j.model.parameter.Value; 1.46 + 1.47 +/** 1.48 + * $Id$ 1.49 + * 1.50 + * Created on 26/06/2005 1.51 + * 1.52 + * Implements a collection of utility methods relevant to date processing. 1.53 + * 1.54 + * @author Ben Fortuna 1.55 + */ 1.56 +public final class Dates { 1.57 + 1.58 + /** 1.59 + * Number of milliseconds in one second. 1.60 + */ 1.61 + public static final long MILLIS_PER_SECOND = 1000; 1.62 + 1.63 + /** 1.64 + * Number of milliseconds in one minute. 1.65 + */ 1.66 + public static final long MILLIS_PER_MINUTE = 60000; 1.67 + 1.68 + /** 1.69 + * Number of milliseconds in one hour. 1.70 + */ 1.71 + public static final long MILLIS_PER_HOUR = 3600000; 1.72 + 1.73 + /** 1.74 + * Number of milliseconds in one day. 1.75 + */ 1.76 + public static final long MILLIS_PER_DAY = 86400000; 1.77 + 1.78 + /** 1.79 + * Number of milliseconds in one week. 1.80 + */ 1.81 + public static final long MILLIS_PER_WEEK = 604800000; 1.82 + 1.83 + /** 1.84 + * Number of days in one week. 1.85 + */ 1.86 + public static final int DAYS_PER_WEEK = 7; 1.87 + 1.88 + /** 1.89 + * Constant indicating precision to the second. 1.90 + */ 1.91 + public static final int PRECISION_SECOND = 0; 1.92 + 1.93 + /** 1.94 + * Constant indicating precision to the day. 1.95 + */ 1.96 + public static final int PRECISION_DAY = 1; 1.97 + 1.98 + /** 1.99 + * Maximum number of weeks per year. 1.100 + */ 1.101 + public static final int MAX_WEEKS_PER_YEAR = 53; 1.102 + 1.103 + /** 1.104 + * Maximum number of days per year. 1.105 + */ 1.106 + public static final int MAX_DAYS_PER_YEAR = 366; 1.107 + 1.108 + /** 1.109 + * Maximum number of days per month. 1.110 + */ 1.111 + public static final int MAX_DAYS_PER_MONTH = 31; 1.112 + 1.113 + private static final String INVALID_WEEK_MESSAGE = "Invalid week number [{0}]"; 1.114 + 1.115 + private static final String INVALID_YEAR_DAY_MESSAGE = "Invalid year day [{0}]"; 1.116 + 1.117 + private static final String INVALID_MONTH_DAY_MESSAGE = "Invalid month day [{0}]"; 1.118 + 1.119 + /** 1.120 + * Constructor made private to prevent instantiation. 1.121 + */ 1.122 + private Dates() { 1.123 + } 1.124 + 1.125 + /** 1.126 + * Returns the absolute week number for the year specified by the 1.127 + * supplied date. Note that a value of zero (0) is invalid for the 1.128 + * weekNo parameter and an <code>IllegalArgumentException</code> 1.129 + * will be thrown. 1.130 + * @param date a date instance representing a week of the year 1.131 + * @param weekNo a week number offset 1.132 + * @return the absolute week of the year for the specified offset 1.133 + */ 1.134 + public static int getAbsWeekNo(final java.util.Date date, final int weekNo) { 1.135 + if (weekNo == 0 || weekNo < -MAX_WEEKS_PER_YEAR || weekNo > MAX_WEEKS_PER_YEAR) { 1.136 + throw new IllegalArgumentException(MessageFormat.format(INVALID_WEEK_MESSAGE, 1.137 + new Object[] {new Integer(weekNo)})); 1.138 + } 1.139 + if (weekNo > 0) { 1.140 + return weekNo; 1.141 + } 1.142 + final Calendar cal = Calendar.getInstance(); 1.143 + cal.setTime(date); 1.144 + final int year = cal.get(Calendar.YEAR); 1.145 + // construct a list of possible week numbers.. 1.146 + final List weeks = new ArrayList(); 1.147 + cal.set(Calendar.WEEK_OF_YEAR, 1); 1.148 + while (cal.get(Calendar.YEAR) == year) { 1.149 + weeks.add(new Integer(cal.get(Calendar.WEEK_OF_YEAR))); 1.150 + cal.add(Calendar.WEEK_OF_YEAR, 1); 1.151 + } 1.152 + return ((Integer) weeks.get(weeks.size() + weekNo)).intValue(); 1.153 + } 1.154 + 1.155 + /** 1.156 + * Returns the absolute year day for the year specified by the 1.157 + * supplied date. Note that a value of zero (0) is invalid for the 1.158 + * yearDay parameter and an <code>IllegalArgumentException</code> 1.159 + * will be thrown. 1.160 + * @param date a date instance representing a day of the year 1.161 + * @param yearDay a day of year offset 1.162 + * @return the absolute day of month for the specified offset 1.163 + */ 1.164 + public static int getAbsYearDay(final java.util.Date date, final int yearDay) { 1.165 + if (yearDay == 0 || yearDay < -MAX_DAYS_PER_YEAR || yearDay > MAX_DAYS_PER_YEAR) { 1.166 + throw new IllegalArgumentException(MessageFormat.format(INVALID_YEAR_DAY_MESSAGE, 1.167 + new Object[] {new Integer(yearDay)})); 1.168 + } 1.169 + if (yearDay > 0) { 1.170 + return yearDay; 1.171 + } 1.172 + final Calendar cal = Calendar.getInstance(); 1.173 + cal.setTime(date); 1.174 + final int year = cal.get(Calendar.YEAR); 1.175 + // construct a list of possible year days.. 1.176 + final List days = new ArrayList(); 1.177 + cal.set(Calendar.DAY_OF_YEAR, 1); 1.178 + while (cal.get(Calendar.YEAR) == year) { 1.179 + days.add(new Integer(cal.get(Calendar.DAY_OF_YEAR))); 1.180 + cal.add(Calendar.DAY_OF_YEAR, 1); 1.181 + } 1.182 + return ((Integer) days.get(days.size() + yearDay)).intValue(); 1.183 + } 1.184 + 1.185 + /** 1.186 + * Returns the absolute month day for the month specified by the 1.187 + * supplied date. Note that a value of zero (0) is invalid for the 1.188 + * monthDay parameter and an <code>IllegalArgumentException</code> 1.189 + * will be thrown. 1.190 + * @param date a date instance representing a day of the month 1.191 + * @param monthDay a day of month offset 1.192 + * @return the absolute day of month for the specified offset 1.193 + */ 1.194 + public static int getAbsMonthDay(final java.util.Date date, final int monthDay) { 1.195 + if (monthDay == 0 || monthDay < -MAX_DAYS_PER_MONTH || monthDay > MAX_DAYS_PER_MONTH) { 1.196 + throw new IllegalArgumentException(MessageFormat.format(INVALID_MONTH_DAY_MESSAGE, 1.197 + new Object[] {new Integer(monthDay)})); 1.198 + } 1.199 + if (monthDay > 0) { 1.200 + return monthDay; 1.201 + } 1.202 + final Calendar cal = Calendar.getInstance(); 1.203 + cal.setTime(date); 1.204 + final int month = cal.get(Calendar.MONTH); 1.205 + // construct a list of possible month days.. 1.206 + final List days = new ArrayList(); 1.207 + cal.set(Calendar.DAY_OF_MONTH, 1); 1.208 + while (cal.get(Calendar.MONTH) == month) { 1.209 + days.add(new Integer(cal.get(Calendar.DAY_OF_MONTH))); 1.210 + cal.add(Calendar.DAY_OF_MONTH, 1); 1.211 + } 1.212 + return ((Integer) days.get(days.size() + monthDay)).intValue(); 1.213 + } 1.214 + 1.215 + /** 1.216 + * Returns a new date instance of the specified type. If no type is 1.217 + * specified a DateTime instance is returned. 1.218 + * @param date a seed Java date instance 1.219 + * @param type the type of date instance 1.220 + * @return an instance of <code>net.fortuna.ical4j.model.Date</code> 1.221 + */ 1.222 + public static Date getInstance(final java.util.Date date, final Value type) { 1.223 + if (Value.DATE.equals(type)) { 1.224 + return new Date(date); 1.225 + } 1.226 + return new DateTime(date); 1.227 + } 1.228 + 1.229 + /** 1.230 + * Returns an instance of <code>java.util.Calendar</code> that is suitably 1.231 + * initialised for working with the specified date. 1.232 + * @param date a date instance 1.233 + * @return a <code>java.util.Calendar</code> 1.234 + */ 1.235 + public static Calendar getCalendarInstance(final Date date) { 1.236 + Calendar instance = null; 1.237 + if (date instanceof DateTime) { 1.238 + final DateTime dateTime = (DateTime) date; 1.239 + if (dateTime.getTimeZone() != null) { 1.240 + instance = Calendar.getInstance(dateTime.getTimeZone()); 1.241 + } 1.242 + else if (dateTime.isUtc()) { 1.243 + instance = Calendar.getInstance(TimeZones.getUtcTimeZone()); 1.244 + } 1.245 + else { 1.246 + // a date-time without a timezone but not UTC is floating 1.247 + instance = Calendar.getInstance(); 1.248 + } 1.249 + } 1.250 + else { 1.251 + instance = Calendar.getInstance(TimeZones.getDateTimeZone()); 1.252 + } 1.253 + return instance; 1.254 + } 1.255 + 1.256 + /** 1.257 + * @param time the time value to round 1.258 + * @param precision the rounding precision 1.259 + * @return a round time value 1.260 + * @deprecated It is not all that useful to perform rounding without specifying an 1.261 + * explicit timezone. 1.262 + */ 1.263 + public static long round(final long time, final int precision) { 1.264 + return round(time, precision, TimeZone.getDefault()); 1.265 +// return round(time, precision, TimeZone.getTimeZone(TimeZones.UTC_ID)); 1.266 + /* 1.267 + long newTime = time; 1.268 + if (precision == PRECISION_DAY) { 1.269 + long remainder = newTime%(1000*60*60); // get the mod remainder using milliseconds*seconds*min 1.270 + newTime = newTime-remainder; 1.271 + // remove the remainder from the time to clear the milliseconds, seconds and minutes 1.272 + } 1.273 + else if (precision == PRECISION_SECOND) { 1.274 + long remainder = newTime%(1000); // get the mod remainder using milliseconds 1.275 + newTime = newTime-remainder; // remove the remainder from the time to clear the milliseconds 1.276 + } 1.277 + return newTime; 1.278 + */ 1.279 + } 1.280 + 1.281 + /** 1.282 + * Rounds a time value to remove any precision smaller than specified. 1.283 + * @param time the time value to round 1.284 + * @param precision the rounding precision 1.285 + * @param tz the timezone of the rounded value 1.286 + * @return a round time value 1.287 + */ 1.288 + public static long round(final long time, final int precision, final TimeZone tz) { 1.289 + if ((precision == PRECISION_SECOND) && ((time % Dates.MILLIS_PER_SECOND) == 0)) { 1.290 + return time; 1.291 + } 1.292 + final Calendar cal = Calendar.getInstance(tz); 1.293 + cal.setTimeInMillis(time); 1.294 + if (precision == PRECISION_DAY) { 1.295 +// return (long) Math.floor(time / (double) Dates.MILLIS_PER_DAY) * Dates.MILLIS_PER_DAY; 1.296 + cal.set(Calendar.HOUR_OF_DAY, 0); 1.297 + cal.clear(Calendar.MINUTE); 1.298 + cal.clear(Calendar.SECOND); 1.299 + cal.clear(Calendar.MILLISECOND); 1.300 + } 1.301 + else if (precision == PRECISION_SECOND) { 1.302 +// return (long) Math.floor(time / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND; 1.303 + cal.clear(Calendar.MILLISECOND); 1.304 + } 1.305 + // unrecognised precision.. 1.306 + return cal.getTimeInMillis(); 1.307 + } 1.308 + 1.309 + /** 1.310 + * Returns the {@code System.currentTimeMillis()}, rounded to the second. 1.311 + * <p>By doing a rough rounding here, we avoid an expensive java.util.Calendar based 1.312 + * rounding later on.</p> 1.313 + * @return the current time in millisec. 1.314 + */ 1.315 + public static long getCurrentTimeRounded() { 1.316 + return (long) Math.floor(System.currentTimeMillis() / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND; 1.317 + } 1.318 +}