src/net/fortuna/ical4j/model/DateTime.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
child 3
73bdfa70b04e
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.model;
michael@0 33
michael@0 34 import java.text.DateFormat;
michael@0 35 import java.text.ParseException;
michael@0 36 import java.text.SimpleDateFormat;
michael@0 37 import java.util.Map;
michael@0 38 import java.util.WeakHashMap;
michael@0 39
michael@0 40 import net.fortuna.ical4j.util.CompatibilityHints;
michael@0 41 import net.fortuna.ical4j.util.Dates;
michael@0 42 import net.fortuna.ical4j.util.TimeZones;
michael@0 43
michael@0 44 import org.apache.commons.lang.builder.EqualsBuilder;
michael@0 45 import org.apache.commons.lang.builder.HashCodeBuilder;
michael@0 46
michael@0 47 /**
michael@0 48 * $Id$
michael@0 49 *
michael@0 50 * Created on 26/06/2005
michael@0 51 *
michael@0 52 * Represents a time of day on a specific date.
michael@0 53 *
michael@0 54 * <pre>
michael@0 55 * 4.3.5 Date-Time
michael@0 56 *
michael@0 57 * Value Name: DATE-TIME
michael@0 58 *
michael@0 59 * Purpose: This value type is used to identify values that specify a
michael@0 60 * precise calendar date and time of day.
michael@0 61 *
michael@0 62 * Formal Definition: The value type is defined by the following
michael@0 63 * notation:
michael@0 64 *
michael@0 65 * date-time = date "T" time ;As specified in the date and time
michael@0 66 * ;value definitions
michael@0 67 *
michael@0 68 * Description: If the property permits, multiple "date-time" values are
michael@0 69 * specified as a COMMA character (US-ASCII decimal 44) separated list
michael@0 70 * of values. No additional content value encoding (i.e., BACKSLASH
michael@0 71 * character encoding) is defined for this value type.
michael@0 72 *
michael@0 73 * The "DATE-TIME" data type is used to identify values that contain a
michael@0 74 * precise calendar date and time of day. The format is based on the
michael@0 75 * [ISO 8601] complete representation, basic format for a calendar date
michael@0 76 * and time of day. The text format is a concatenation of the "date",
michael@0 77 * followed by the LATIN CAPITAL LETTER T character (US-ASCII decimal
michael@0 78 * 84) time designator, followed by the "time" format.
michael@0 79 *
michael@0 80 * The "DATE-TIME" data type expresses time values in three forms:
michael@0 81 *
michael@0 82 * The form of date and time with UTC offset MUST NOT be used. For
michael@0 83 * example, the following is not valid for a date-time value:
michael@0 84 *
michael@0 85 * DTSTART:19980119T230000-0800 ;Invalid time format
michael@0 86 *
michael@0 87 * FORM #1: DATE WITH LOCAL TIME
michael@0 88 *
michael@0 89 * The date with local time form is simply a date-time value that does
michael@0 90 * not contain the UTC designator nor does it reference a time zone. For
michael@0 91 * example, the following represents Janurary 18, 1998, at 11 PM:
michael@0 92 *
michael@0 93 * DTSTART:19980118T230000
michael@0 94 *
michael@0 95 * Date-time values of this type are said to be "floating" and are not
michael@0 96 * bound to any time zone in particular. They are used to represent the
michael@0 97 * same hour, minute, and second value regardless of which time zone is
michael@0 98 * currently being observed. For example, an event can be defined that
michael@0 99 * indicates that an individual will be busy from 11:00 AM to 1:00 PM
michael@0 100 * every day, no matter which time zone the person is in. In these
michael@0 101 * cases, a local time can be specified. The recipient of an iCalendar
michael@0 102 * object with a property value consisting of a local time, without any
michael@0 103 * relative time zone information, SHOULD interpret the value as being
michael@0 104 * fixed to whatever time zone the ATTENDEE is in at any given moment.
michael@0 105 * This means that two ATTENDEEs, in different time zones, receiving the
michael@0 106 * same event definition as a floating time, may be participating in the
michael@0 107 * event at different actual times. Floating time SHOULD only be used
michael@0 108 * where that is the reasonable behavior.
michael@0 109 *
michael@0 110 * In most cases, a fixed time is desired. To properly communicate a
michael@0 111 * fixed time in a property value, either UTC time or local time with
michael@0 112 * time zone reference MUST be specified.
michael@0 113 *
michael@0 114 * The use of local time in a DATE-TIME value without the TZID property
michael@0 115 * parameter is to be interpreted as floating time, regardless of the
michael@0 116 * existence of "VTIMEZONE" calendar components in the iCalendar object.
michael@0 117 *
michael@0 118 * FORM #2: DATE WITH UTC TIME
michael@0 119 *
michael@0 120 * The date with UTC time, or absolute time, is identified by a LATIN
michael@0 121 * CAPITAL LETTER Z suffix character (US-ASCII decimal 90), the UTC
michael@0 122 * designator, appended to the time value. For example, the following
michael@0 123 * represents January 19, 1998, at 0700 UTC:
michael@0 124 *
michael@0 125 * DTSTART:19980119T070000Z
michael@0 126 *
michael@0 127 * The TZID property parameter MUST NOT be applied to DATE-TIME
michael@0 128 * properties whose time values are specified in UTC.
michael@0 129 *
michael@0 130 * FORM #3: DATE WITH LOCAL TIME AND TIME ZONE REFERENCE
michael@0 131 *
michael@0 132 * The date and local time with reference to time zone information is
michael@0 133 * identified by the use the TZID property parameter to reference the
michael@0 134 * appropriate time zone definition. TZID is discussed in detail in the
michael@0 135 * section on Time Zone. For example, the following represents 2 AM in
michael@0 136 * New York on Janurary 19, 1998:
michael@0 137 *
michael@0 138 * DTSTART;TZID=US-Eastern:19980119T020000
michael@0 139 *
michael@0 140 * Example: The following represents July 14, 1997, at 1:30 PM in New
michael@0 141 * York City in each of the three time formats, using the "DTSTART"
michael@0 142 * property.
michael@0 143 *
michael@0 144 * DTSTART:19970714T133000 ;Local time
michael@0 145 * DTSTART:19970714T173000Z ;UTC time
michael@0 146 * DTSTART;TZID=US-Eastern:19970714T133000 ;Local time and time
michael@0 147 * ; zone reference
michael@0 148 *
michael@0 149 * A time value MUST ONLY specify 60 seconds when specifying the
michael@0 150 * periodic "leap second" in the time value. For example:
michael@0 151 *
michael@0 152 * COMPLETED:19970630T235960Z
michael@0 153 * </pre>
michael@0 154 *
michael@0 155 * @author Ben Fortuna
michael@0 156 */
michael@0 157 public class DateTime extends Date {
michael@0 158
michael@0 159 private static final long serialVersionUID = -6407231357919440387L;
michael@0 160
michael@0 161 private static final String DEFAULT_PATTERN = "yyyyMMdd'T'HHmmss";
michael@0 162
michael@0 163 private static final String UTC_PATTERN = "yyyyMMdd'T'HHmmss'Z'";
michael@0 164
michael@0 165 private static final String RELAXED_PATTERN = "yyyyMMdd";
michael@0 166
michael@0 167 /**
michael@0 168 * Used for parsing times in a UTC date-time representation.
michael@0 169 */
michael@0 170 private static final DateFormatCache UTC_FORMAT;
michael@0 171 static {
michael@0 172 final DateFormat format = new SimpleDateFormat(UTC_PATTERN);
michael@0 173 format.setTimeZone(TimeZones.getUtcTimeZone());
michael@0 174 format.setLenient(false);
michael@0 175
michael@0 176 UTC_FORMAT = new DateFormatCache(format);
michael@0 177 }
michael@0 178
michael@0 179 /**
michael@0 180 * Used for parsing times in a local date-time representation.
michael@0 181 */
michael@0 182 private static final DateFormatCache DEFAULT_FORMAT;
michael@0 183 static {
michael@0 184 final DateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
michael@0 185 format.setLenient(false);
michael@0 186 DEFAULT_FORMAT = new DateFormatCache(format);
michael@0 187 }
michael@0 188
michael@0 189 private static final DateFormatCache LENIENT_DEFAULT_FORMAT;
michael@0 190 static {
michael@0 191 final DateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
michael@0 192 LENIENT_DEFAULT_FORMAT = new DateFormatCache(format);
michael@0 193 }
michael@0 194
michael@0 195 private static final DateFormatCache RELAXED_FORMAT;
michael@0 196 static {
michael@0 197 final DateFormat format = new SimpleDateFormat(RELAXED_PATTERN);
michael@0 198 format.setLenient(false);
michael@0 199 RELAXED_FORMAT = new DateFormatCache(format);
michael@0 200 }
michael@0 201
michael@0 202 private Time time;
michael@0 203
michael@0 204 private TimeZone timezone;
michael@0 205
michael@0 206 /**
michael@0 207 * Default constructor.
michael@0 208 */
michael@0 209 public DateTime() {
michael@0 210 super(Dates.PRECISION_SECOND, java.util.TimeZone.getDefault());
michael@0 211 this.time = new Time(getTime(), getFormat().getTimeZone());
michael@0 212 }
michael@0 213
michael@0 214 /**
michael@0 215 * @param utc
michael@0 216 * indicates if the date is in UTC time
michael@0 217 */
michael@0 218 public DateTime(final boolean utc) {
michael@0 219 this();
michael@0 220 setUtc(utc);
michael@0 221 }
michael@0 222
michael@0 223 /**
michael@0 224 * @param time
michael@0 225 * a date-time value in milliseconds
michael@0 226 */
michael@0 227 public DateTime(final long time) {
michael@0 228 super(time, Dates.PRECISION_SECOND, java.util.TimeZone.getDefault());
michael@0 229 this.time = new Time(time, getFormat().getTimeZone());
michael@0 230 }
michael@0 231
michael@0 232 /**
michael@0 233 * @param date
michael@0 234 * a date-time value
michael@0 235 */
michael@0 236 public DateTime(final java.util.Date date) {
michael@0 237 super(date.getTime(), Dates.PRECISION_SECOND, java.util.TimeZone.getDefault());
michael@0 238 this.time = new Time(date.getTime(), getFormat().getTimeZone());
michael@0 239 // copy timezone information if applicable..
michael@0 240 if (date instanceof DateTime) {
michael@0 241 final DateTime dateTime = (DateTime) date;
michael@0 242 if (dateTime.isUtc()) {
michael@0 243 setUtc(true);
michael@0 244 } else {
michael@0 245 setTimeZone(dateTime.getTimeZone());
michael@0 246 }
michael@0 247 }
michael@0 248 }
michael@0 249
michael@0 250 /**
michael@0 251 * Constructs a new DateTime instance from parsing the specified string
michael@0 252 * representation in the default (local) timezone.
michael@0 253 *
michael@0 254 * @param value
michael@0 255 * a string representation of a date-time
michael@0 256 * @throws ParseException
michael@0 257 * where the specified string is not a valid date-time
michael@0 258 */
michael@0 259 public DateTime(final String value) throws ParseException {
michael@0 260 this(value, null);
michael@0 261 /*
michael@0 262 * long time = 0; try { synchronized (UTC_FORMAT) { time =
michael@0 263 * UTC_FORMAT.parse(value).getTime(); } setUtc(true); } catch
michael@0 264 * (ParseException pe) { synchronized (DEFAULT_FORMAT) {
michael@0 265 * DEFAULT_FORMAT.setTimeZone(getFormat().getTimeZone()); time =
michael@0 266 * DEFAULT_FORMAT.parse(value).getTime(); } this.time = new Time(time,
michael@0 267 * getFormat().getTimeZone()); } setTime(time);
michael@0 268 */
michael@0 269 }
michael@0 270
michael@0 271 /**
michael@0 272 * Creates a new date-time instance from the specified value in the given
michael@0 273 * timezone. If a timezone is not specified, the default timezone (as
michael@0 274 * returned by {@link java.util.TimeZone#getDefault()}) is used.
michael@0 275 *
michael@0 276 * @param value
michael@0 277 * a string representation of a date-time
michael@0 278 * @param timezone
michael@0 279 * the timezone for the date-time instance
michael@0 280 * @throws ParseException
michael@0 281 * where the specified string is not a valid date-time
michael@0 282 */
michael@0 283 public DateTime(final String value, final TimeZone timezone)
michael@0 284 throws ParseException {
michael@0 285 // setting the time to 0 since we are going to reset it anyway
michael@0 286 super(0, Dates.PRECISION_SECOND, timezone != null ? timezone
michael@0 287 : java.util.TimeZone.getDefault());
michael@0 288 this.time = new Time(getTime(), getFormat().getTimeZone());
michael@0 289
michael@0 290 try {
michael@0 291 if (value.endsWith("Z")) {
michael@0 292 setTime(value, (DateFormat) UTC_FORMAT.get(), null);
michael@0 293 setUtc(true);
michael@0 294 } else {
michael@0 295 if (timezone != null) {
michael@0 296 setTime(value, (DateFormat) DEFAULT_FORMAT.get(), timezone);
michael@0 297 } else {
michael@0 298 // Use lenient parsing for floating times. This is to
michael@0 299 // overcome
michael@0 300 // the problem of parsing VTimeZone dates that specify dates
michael@0 301 // that the strict parser does not accept.
michael@0 302 setTime(value, (DateFormat) LENIENT_DEFAULT_FORMAT.get(),
michael@0 303 getFormat().getTimeZone());
michael@0 304 }
michael@0 305 setTimeZone(timezone);
michael@0 306 }
michael@0 307 } catch (ParseException pe) {
michael@0 308 if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
michael@0 309
michael@0 310 setTime(value, (DateFormat) RELAXED_FORMAT.get(), timezone);
michael@0 311 setTimeZone(timezone);
michael@0 312 } else {
michael@0 313 throw pe;
michael@0 314 }
michael@0 315 }
michael@0 316 }
michael@0 317
michael@0 318 /**
michael@0 319 * @param value
michael@0 320 * a string representation of a date-time
michael@0 321 * @param pattern
michael@0 322 * a pattern to apply when parsing the date-time value
michael@0 323 * @param timezone
michael@0 324 * the timezone for the date-time instance
michael@0 325 * @throws ParseException
michael@0 326 * where the specified string is not a valid date-time
michael@0 327 */
michael@0 328 public DateTime(String value, String pattern, TimeZone timezone)
michael@0 329 throws ParseException {
michael@0 330 // setting the time to 0 since we are going to reset it anyway
michael@0 331 super(0, Dates.PRECISION_SECOND, timezone != null ? timezone
michael@0 332 : java.util.TimeZone.getDefault());
michael@0 333 this.time = new Time(getTime(), getFormat().getTimeZone());
michael@0 334
michael@0 335 final DateFormat format = CalendarDateFormatFactory
michael@0 336 .getInstance(pattern);
michael@0 337 setTime(value, format, timezone);
michael@0 338 }
michael@0 339
michael@0 340 /**
michael@0 341 * @param value
michael@0 342 * a string representation of a date-time
michael@0 343 * @param pattern
michael@0 344 * a pattern to apply when parsing the date-time value
michael@0 345 * @param utc
michael@0 346 * indicates whether the date-time is in UTC time
michael@0 347 * @throws ParseException
michael@0 348 * where the specified string is not a valid date-time
michael@0 349 */
michael@0 350 public DateTime(String value, String pattern, boolean utc)
michael@0 351 throws ParseException {
michael@0 352 // setting the time to 0 since we are going to reset it anyway
michael@0 353 this(0);
michael@0 354 final DateFormat format = CalendarDateFormatFactory
michael@0 355 .getInstance(pattern);
michael@0 356 if (utc) {
michael@0 357 setTime(value, format,
michael@0 358 ((DateFormat) UTC_FORMAT.get()).getTimeZone());
michael@0 359 } else {
michael@0 360 setTime(value, format, null);
michael@0 361 }
michael@0 362 setUtc(utc);
michael@0 363 }
michael@0 364
michael@0 365 /**
michael@0 366 * Internal set of time by parsing value string.
michael@0 367 *
michael@0 368 * @param value
michael@0 369 * @param format
michael@0 370 * a {@code DateFormat}, protected by the use of a ThreadLocal.
michael@0 371 * @param tz
michael@0 372 * @throws ParseException
michael@0 373 */
michael@0 374 private void setTime(final String value, final DateFormat format,
michael@0 375 final java.util.TimeZone tz) throws ParseException {
michael@0 376
michael@0 377 if (tz != null) {
michael@0 378 format.setTimeZone(tz);
michael@0 379 }
michael@0 380 setTime(format.parse(value).getTime());
michael@0 381 }
michael@0 382
michael@0 383 /**
michael@0 384 * {@inheritDoc}
michael@0 385 */
michael@0 386 public final void setTime(final long time) {
michael@0 387 super.setTime(time);
michael@0 388 // need to check for null time due to Android java.util.Date(long)
michael@0 389 // constructor
michael@0 390 // calling this method..
michael@0 391 if (this.time != null) {
michael@0 392 this.time.setTime(time);
michael@0 393 }
michael@0 394 }
michael@0 395
michael@0 396 /**
michael@0 397 * @return Returns the utc.
michael@0 398 */
michael@0 399 public final boolean isUtc() {
michael@0 400 return time.isUtc();
michael@0 401 }
michael@0 402
michael@0 403 /**
michael@0 404 * Updates this date-time to display in UTC time if the argument is true.
michael@0 405 * Otherwise, resets to the default timezone.
michael@0 406 *
michael@0 407 * @param utc
michael@0 408 * The utc to set.
michael@0 409 */
michael@0 410 public final void setUtc(final boolean utc) {
michael@0 411 // reset the timezone associated with this instance..
michael@0 412 this.timezone = null;
michael@0 413 if (utc) {
michael@0 414 getFormat().setTimeZone(TimeZones.getUtcTimeZone());
michael@0 415 } else {
michael@0 416 resetTimeZone();
michael@0 417 }
michael@0 418 time = new Time(time, getFormat().getTimeZone(), utc);
michael@0 419 }
michael@0 420
michael@0 421 /**
michael@0 422 * Sets the timezone associated with this date-time instance. If the
michael@0 423 * specified timezone is null, it will reset to the default timezone. If the
michael@0 424 * date-time instance is utc, it will turn into either a floating (no
michael@0 425 * timezone) date-time, or a date-time with a timezone.
michael@0 426 *
michael@0 427 * @param timezone
michael@0 428 * a timezone to apply to the instance
michael@0 429 */
michael@0 430 public final void setTimeZone(final TimeZone timezone) {
michael@0 431 this.timezone = timezone;
michael@0 432 if (timezone != null) {
michael@0 433 getFormat().setTimeZone(timezone);
michael@0 434 } else {
michael@0 435 resetTimeZone();
michael@0 436 }
michael@0 437 time = new Time(time, getFormat().getTimeZone(), false);
michael@0 438 }
michael@0 439
michael@0 440 /**
michael@0 441 * Reset the timezone to default.
michael@0 442 */
michael@0 443 private void resetTimeZone() {
michael@0 444 // use GMT timezone to avoid daylight savings rules affecting floating
michael@0 445 // time values..
michael@0 446 getFormat().setTimeZone(TimeZone.getDefault());
michael@0 447 // getFormat().setTimeZone(TimeZone.getTimeZone(TimeZones.GMT_ID));
michael@0 448 }
michael@0 449
michael@0 450 /**
michael@0 451 * Returns the current timezone associated with this date-time value.
michael@0 452 *
michael@0 453 * @return a Java timezone
michael@0 454 */
michael@0 455 public final TimeZone getTimeZone() {
michael@0 456 return timezone;
michael@0 457 }
michael@0 458
michael@0 459 /**
michael@0 460 * {@inheritDoc}
michael@0 461 */
michael@0 462 public final String toString() {
michael@0 463 final StringBuffer b = new StringBuffer(super.toString());
michael@0 464 b.append('T');
michael@0 465 b.append(time.toString());
michael@0 466 return b.toString();
michael@0 467 }
michael@0 468
michael@0 469 /**
michael@0 470 * {@inheritDoc}
michael@0 471 */
michael@0 472 public boolean equals(final Object arg0) {
michael@0 473 // TODO: what about compareTo, before, after, etc.?
michael@0 474
michael@0 475 if (arg0 instanceof DateTime) {
michael@0 476 return new EqualsBuilder().append(time, ((DateTime) arg0).time)
michael@0 477 .isEquals();
michael@0 478 }
michael@0 479 return super.equals(arg0);
michael@0 480 }
michael@0 481
michael@0 482 /**
michael@0 483 * {@inheritDoc}
michael@0 484 */
michael@0 485 public int hashCode() {
michael@0 486 return super.hashCode();
michael@0 487 }
michael@0 488
michael@0 489 private static class DateFormatCache {
michael@0 490
michael@0 491 private final Map threadMap = new WeakHashMap();
michael@0 492
michael@0 493 private final DateFormat templateFormat;
michael@0 494
michael@0 495 private DateFormatCache(DateFormat dateFormat) {
michael@0 496 this.templateFormat = dateFormat;
michael@0 497 }
michael@0 498
michael@0 499 public DateFormat get() {
michael@0 500 DateFormat dateFormat = (DateFormat) threadMap.get(Thread
michael@0 501 .currentThread());
michael@0 502 if (dateFormat == null) {
michael@0 503 dateFormat = (DateFormat) templateFormat.clone();
michael@0 504 threadMap.put(Thread.currentThread(), dateFormat);
michael@0 505 }
michael@0 506 return dateFormat;
michael@0 507 }
michael@0 508 }
michael@0 509 }

mercurial