src/net/fortuna/ical4j/model/DateTime.java

Tue, 10 Feb 2015 19:38:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:38:00 +0100
changeset 3
73bdfa70b04e
parent 0
fb9019fb1bf7
child 4
45d57ecba757
permissions
-rw-r--r--

Upgrade embedded ical4j from ancient whatever to upstream version 1.0.6.

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

mercurial