src/net/fortuna/ical4j/util/Dates.java

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

michael@0 1 /**
michael@0 2 * Copyright (c) 2012, Ben Fortuna
michael@0 3 * All rights reserved.
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted provided that the following conditions
michael@0 7 * are met:
michael@0 8 *
michael@0 9 * o Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 *
michael@0 12 * o Redistributions in binary form must reproduce the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer in the
michael@0 14 * documentation and/or other materials provided with the distribution.
michael@0 15 *
michael@0 16 * o Neither the name of Ben Fortuna nor the names of any other contributors
michael@0 17 * may be used to endorse or promote products derived from this software
michael@0 18 * without specific prior written permission.
michael@0 19 *
michael@0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32 package net.fortuna.ical4j.util;
michael@0 33
michael@0 34 import java.text.MessageFormat;
michael@0 35 import java.util.ArrayList;
michael@0 36 import java.util.Calendar;
michael@0 37 import java.util.List;
michael@0 38 import java.util.TimeZone;
michael@0 39
michael@0 40 import net.fortuna.ical4j.model.Date;
michael@0 41 import net.fortuna.ical4j.model.DateTime;
michael@0 42 import net.fortuna.ical4j.model.parameter.Value;
michael@0 43
michael@0 44 /**
michael@0 45 * $Id$
michael@0 46 *
michael@0 47 * Created on 26/06/2005
michael@0 48 *
michael@0 49 * Implements a collection of utility methods relevant to date processing.
michael@0 50 *
michael@0 51 * @author Ben Fortuna
michael@0 52 */
michael@0 53 public final class Dates {
michael@0 54
michael@0 55 /**
michael@0 56 * Number of milliseconds in one second.
michael@0 57 */
michael@0 58 public static final long MILLIS_PER_SECOND = 1000;
michael@0 59
michael@0 60 /**
michael@0 61 * Number of milliseconds in one minute.
michael@0 62 */
michael@0 63 public static final long MILLIS_PER_MINUTE = 60000;
michael@0 64
michael@0 65 /**
michael@0 66 * Number of milliseconds in one hour.
michael@0 67 */
michael@0 68 public static final long MILLIS_PER_HOUR = 3600000;
michael@0 69
michael@0 70 /**
michael@0 71 * Number of milliseconds in one day.
michael@0 72 */
michael@0 73 public static final long MILLIS_PER_DAY = 86400000;
michael@0 74
michael@0 75 /**
michael@0 76 * Number of milliseconds in one week.
michael@0 77 */
michael@0 78 public static final long MILLIS_PER_WEEK = 604800000;
michael@0 79
michael@0 80 /**
michael@0 81 * Number of days in one week.
michael@0 82 */
michael@0 83 public static final int DAYS_PER_WEEK = 7;
michael@0 84
michael@0 85 /**
michael@0 86 * Constant indicating precision to the second.
michael@0 87 */
michael@0 88 public static final int PRECISION_SECOND = 0;
michael@0 89
michael@0 90 /**
michael@0 91 * Constant indicating precision to the day.
michael@0 92 */
michael@0 93 public static final int PRECISION_DAY = 1;
michael@0 94
michael@0 95 /**
michael@0 96 * Maximum number of weeks per year.
michael@0 97 */
michael@0 98 public static final int MAX_WEEKS_PER_YEAR = 53;
michael@0 99
michael@0 100 /**
michael@0 101 * Maximum number of days per year.
michael@0 102 */
michael@0 103 public static final int MAX_DAYS_PER_YEAR = 366;
michael@0 104
michael@0 105 /**
michael@0 106 * Maximum number of days per month.
michael@0 107 */
michael@0 108 public static final int MAX_DAYS_PER_MONTH = 31;
michael@0 109
michael@0 110 private static final String INVALID_WEEK_MESSAGE = "Invalid week number [{0}]";
michael@0 111
michael@0 112 private static final String INVALID_YEAR_DAY_MESSAGE = "Invalid year day [{0}]";
michael@0 113
michael@0 114 private static final String INVALID_MONTH_DAY_MESSAGE = "Invalid month day [{0}]";
michael@0 115
michael@0 116 /**
michael@0 117 * Constructor made private to prevent instantiation.
michael@0 118 */
michael@0 119 private Dates() {
michael@0 120 }
michael@0 121
michael@0 122 /**
michael@0 123 * Returns the absolute week number for the year specified by the
michael@0 124 * supplied date. Note that a value of zero (0) is invalid for the
michael@0 125 * weekNo parameter and an <code>IllegalArgumentException</code>
michael@0 126 * will be thrown.
michael@0 127 * @param date a date instance representing a week of the year
michael@0 128 * @param weekNo a week number offset
michael@0 129 * @return the absolute week of the year for the specified offset
michael@0 130 */
michael@0 131 public static int getAbsWeekNo(final java.util.Date date, final int weekNo) {
michael@0 132 if (weekNo == 0 || weekNo < -MAX_WEEKS_PER_YEAR || weekNo > MAX_WEEKS_PER_YEAR) {
michael@0 133 throw new IllegalArgumentException(MessageFormat.format(INVALID_WEEK_MESSAGE,
michael@0 134 new Object[] {new Integer(weekNo)}));
michael@0 135 }
michael@0 136 if (weekNo > 0) {
michael@0 137 return weekNo;
michael@0 138 }
michael@0 139 final Calendar cal = Calendar.getInstance();
michael@0 140 cal.setTime(date);
michael@0 141 final int year = cal.get(Calendar.YEAR);
michael@0 142 // construct a list of possible week numbers..
michael@0 143 final List weeks = new ArrayList();
michael@0 144 cal.set(Calendar.WEEK_OF_YEAR, 1);
michael@0 145 while (cal.get(Calendar.YEAR) == year) {
michael@0 146 weeks.add(new Integer(cal.get(Calendar.WEEK_OF_YEAR)));
michael@0 147 cal.add(Calendar.WEEK_OF_YEAR, 1);
michael@0 148 }
michael@0 149 return ((Integer) weeks.get(weeks.size() + weekNo)).intValue();
michael@0 150 }
michael@0 151
michael@0 152 /**
michael@0 153 * Returns the absolute year day for the year specified by the
michael@0 154 * supplied date. Note that a value of zero (0) is invalid for the
michael@0 155 * yearDay parameter and an <code>IllegalArgumentException</code>
michael@0 156 * will be thrown.
michael@0 157 * @param date a date instance representing a day of the year
michael@0 158 * @param yearDay a day of year offset
michael@0 159 * @return the absolute day of month for the specified offset
michael@0 160 */
michael@0 161 public static int getAbsYearDay(final java.util.Date date, final int yearDay) {
michael@0 162 if (yearDay == 0 || yearDay < -MAX_DAYS_PER_YEAR || yearDay > MAX_DAYS_PER_YEAR) {
michael@0 163 throw new IllegalArgumentException(MessageFormat.format(INVALID_YEAR_DAY_MESSAGE,
michael@0 164 new Object[] {new Integer(yearDay)}));
michael@0 165 }
michael@0 166 if (yearDay > 0) {
michael@0 167 return yearDay;
michael@0 168 }
michael@0 169 final Calendar cal = Calendar.getInstance();
michael@0 170 cal.setTime(date);
michael@0 171 final int year = cal.get(Calendar.YEAR);
michael@0 172 // construct a list of possible year days..
michael@0 173 final List days = new ArrayList();
michael@0 174 cal.set(Calendar.DAY_OF_YEAR, 1);
michael@0 175 while (cal.get(Calendar.YEAR) == year) {
michael@0 176 days.add(new Integer(cal.get(Calendar.DAY_OF_YEAR)));
michael@0 177 cal.add(Calendar.DAY_OF_YEAR, 1);
michael@0 178 }
michael@0 179 return ((Integer) days.get(days.size() + yearDay)).intValue();
michael@0 180 }
michael@0 181
michael@0 182 /**
michael@0 183 * Returns the absolute month day for the month specified by the
michael@0 184 * supplied date. Note that a value of zero (0) is invalid for the
michael@0 185 * monthDay parameter and an <code>IllegalArgumentException</code>
michael@0 186 * will be thrown.
michael@0 187 * @param date a date instance representing a day of the month
michael@0 188 * @param monthDay a day of month offset
michael@0 189 * @return the absolute day of month for the specified offset
michael@0 190 */
michael@0 191 public static int getAbsMonthDay(final java.util.Date date, final int monthDay) {
michael@0 192 if (monthDay == 0 || monthDay < -MAX_DAYS_PER_MONTH || monthDay > MAX_DAYS_PER_MONTH) {
michael@0 193 throw new IllegalArgumentException(MessageFormat.format(INVALID_MONTH_DAY_MESSAGE,
michael@0 194 new Object[] {new Integer(monthDay)}));
michael@0 195 }
michael@0 196 if (monthDay > 0) {
michael@0 197 return monthDay;
michael@0 198 }
michael@0 199 final Calendar cal = Calendar.getInstance();
michael@0 200 cal.setTime(date);
michael@0 201 final int month = cal.get(Calendar.MONTH);
michael@0 202 // construct a list of possible month days..
michael@0 203 final List days = new ArrayList();
michael@0 204 cal.set(Calendar.DAY_OF_MONTH, 1);
michael@0 205 while (cal.get(Calendar.MONTH) == month) {
michael@0 206 days.add(new Integer(cal.get(Calendar.DAY_OF_MONTH)));
michael@0 207 cal.add(Calendar.DAY_OF_MONTH, 1);
michael@0 208 }
michael@0 209 return ((Integer) days.get(days.size() + monthDay)).intValue();
michael@0 210 }
michael@0 211
michael@0 212 /**
michael@0 213 * Returns a new date instance of the specified type. If no type is
michael@0 214 * specified a DateTime instance is returned.
michael@0 215 * @param date a seed Java date instance
michael@0 216 * @param type the type of date instance
michael@0 217 * @return an instance of <code>net.fortuna.ical4j.model.Date</code>
michael@0 218 */
michael@0 219 public static Date getInstance(final java.util.Date date, final Value type) {
michael@0 220 if (Value.DATE.equals(type)) {
michael@0 221 return new Date(date);
michael@0 222 }
michael@0 223 return new DateTime(date);
michael@0 224 }
michael@0 225
michael@0 226 /**
michael@0 227 * Returns an instance of <code>java.util.Calendar</code> that is suitably
michael@0 228 * initialised for working with the specified date.
michael@0 229 * @param date a date instance
michael@0 230 * @return a <code>java.util.Calendar</code>
michael@0 231 */
michael@0 232 public static Calendar getCalendarInstance(final Date date) {
michael@0 233 Calendar instance = null;
michael@0 234 if (date instanceof DateTime) {
michael@0 235 final DateTime dateTime = (DateTime) date;
michael@0 236 if (dateTime.getTimeZone() != null) {
michael@0 237 instance = Calendar.getInstance(dateTime.getTimeZone());
michael@0 238 }
michael@0 239 else if (dateTime.isUtc()) {
michael@0 240 instance = Calendar.getInstance(TimeZones.getUtcTimeZone());
michael@0 241 }
michael@0 242 else {
michael@0 243 // a date-time without a timezone but not UTC is floating
michael@0 244 instance = Calendar.getInstance();
michael@0 245 }
michael@0 246 }
michael@0 247 else {
michael@0 248 instance = Calendar.getInstance(TimeZones.getDateTimeZone());
michael@0 249 }
michael@0 250 return instance;
michael@0 251 }
michael@0 252
michael@0 253 /**
michael@0 254 * @param time the time value to round
michael@0 255 * @param precision the rounding precision
michael@0 256 * @return a round time value
michael@0 257 * @deprecated It is not all that useful to perform rounding without specifying an
michael@0 258 * explicit timezone.
michael@0 259 */
michael@0 260 public static long round(final long time, final int precision) {
michael@0 261 return round(time, precision, TimeZone.getDefault());
michael@0 262 // return round(time, precision, TimeZone.getTimeZone(TimeZones.UTC_ID));
michael@0 263 /*
michael@0 264 long newTime = time;
michael@0 265 if (precision == PRECISION_DAY) {
michael@0 266 long remainder = newTime%(1000*60*60); // get the mod remainder using milliseconds*seconds*min
michael@0 267 newTime = newTime-remainder;
michael@0 268 // remove the remainder from the time to clear the milliseconds, seconds and minutes
michael@0 269 }
michael@0 270 else if (precision == PRECISION_SECOND) {
michael@0 271 long remainder = newTime%(1000); // get the mod remainder using milliseconds
michael@0 272 newTime = newTime-remainder; // remove the remainder from the time to clear the milliseconds
michael@0 273 }
michael@0 274 return newTime;
michael@0 275 */
michael@0 276 }
michael@0 277
michael@0 278 /**
michael@0 279 * Rounds a time value to remove any precision smaller than specified.
michael@0 280 * @param time the time value to round
michael@0 281 * @param precision the rounding precision
michael@0 282 * @param tz the timezone of the rounded value
michael@0 283 * @return a round time value
michael@0 284 */
michael@0 285 public static long round(final long time, final int precision, final TimeZone tz) {
michael@0 286 if ((precision == PRECISION_SECOND) && ((time % Dates.MILLIS_PER_SECOND) == 0)) {
michael@0 287 return time;
michael@0 288 }
michael@0 289 final Calendar cal = Calendar.getInstance(tz);
michael@0 290 cal.setTimeInMillis(time);
michael@0 291 if (precision == PRECISION_DAY) {
michael@0 292 // return (long) Math.floor(time / (double) Dates.MILLIS_PER_DAY) * Dates.MILLIS_PER_DAY;
michael@0 293 cal.set(Calendar.HOUR_OF_DAY, 0);
michael@0 294 cal.clear(Calendar.MINUTE);
michael@0 295 cal.clear(Calendar.SECOND);
michael@0 296 cal.clear(Calendar.MILLISECOND);
michael@0 297 }
michael@0 298 else if (precision == PRECISION_SECOND) {
michael@0 299 // return (long) Math.floor(time / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND;
michael@0 300 cal.clear(Calendar.MILLISECOND);
michael@0 301 }
michael@0 302 // unrecognised precision..
michael@0 303 return cal.getTimeInMillis();
michael@0 304 }
michael@0 305
michael@0 306 /**
michael@0 307 * Returns the {@code System.currentTimeMillis()}, rounded to the second.
michael@0 308 * <p>By doing a rough rounding here, we avoid an expensive java.util.Calendar based
michael@0 309 * rounding later on.</p>
michael@0 310 * @return the current time in millisec.
michael@0 311 */
michael@0 312 public static long getCurrentTimeRounded() {
michael@0 313 return (long) Math.floor(System.currentTimeMillis() / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND;
michael@0 314 }
michael@0 315 }

mercurial