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.model; michael@0: michael@3: import java.text.DateFormat; michael@0: import java.text.ParseException; michael@3: import java.text.SimpleDateFormat; michael@0: import java.util.TimeZone; michael@0: michael@3: import net.fortuna.ical4j.util.CompatibilityHints; michael@0: import net.fortuna.ical4j.util.Dates; michael@0: import net.fortuna.ical4j.util.TimeZones; michael@0: michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created on 26/06/2005 michael@0: * michael@0: * Base class for all representations of time values in RFC2445. michael@0: * michael@0: *
michael@0: * 4.3.4 Date michael@0: * michael@0: * Value Name: DATE michael@0: * michael@0: * Purpose: This value type is used to identify values that contain a michael@0: * calendar date. michael@0: * michael@0: * Formal Definition: The value type is defined by the following michael@0: * notation: michael@0: * michael@0: * date = date-value michael@0: * michael@0: * date-value = date-fullyear date-month date-mday michael@0: * date-fullyear = 4DIGIT michael@0: * date-month = 2DIGIT ;01-12 michael@0: * date-mday = 2DIGIT ;01-28, 01-29, 01-30, 01-31 michael@0: * ;based on month/year michael@0: * michael@0: * Description: If the property permits, multiple "date" values are michael@0: * specified as a COMMA character (US-ASCII decimal 44) separated list michael@0: * of values. The format for the value type is expressed as the [ISO michael@0: * 8601] complete representation, basic format for a calendar date. The michael@0: * textual format specifies a four-digit year, two-digit month, and michael@0: * two-digit day of the month. There are no separator characters between michael@0: * the year, month and day component text. michael@0: * michael@0: * No additional content value encoding (i.e., BACKSLASH character michael@0: * encoding) is defined for this value type. michael@0: * michael@0: * Example: The following represents July 14, 1997: michael@0: * michael@0: * 19970714 michael@0: * michael@0: *michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class Date extends Iso8601 { michael@0: michael@0: private static final long serialVersionUID = 7136072363141363141L; michael@0: michael@3: private static final String DEFAULT_PATTERN = "yyyyMMdd"; michael@3: michael@3: private static final String VCARD_PATTERN = "yyyy'-'MM'-'dd"; michael@0: michael@0: /** michael@0: * Default constructor. michael@0: */ michael@0: public Date() { michael@3: super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); michael@0: } michael@0: michael@0: /** michael@0: * Creates a new date instance with the specified precision. This michael@0: * constructor is only intended for use by sub-classes. michael@0: * @param precision the date precision michael@0: * @param tz the timezone michael@0: * @see Dates#PRECISION_DAY michael@0: * @see Dates#PRECISION_SECOND michael@0: */ michael@0: protected Date(final int precision, TimeZone tz) { michael@3: super(DEFAULT_PATTERN, precision, tz); michael@0: } michael@0: michael@0: /** michael@0: * @param time a date value in milliseconds michael@0: */ michael@0: public Date(final long time) { michael@3: super(time, DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); michael@0: } michael@0: michael@0: /** michael@0: * Creates a new date instance with the specified precision. This michael@0: * constructor is only intended for use by sub-classes. michael@0: * @param time a date value in milliseconds michael@0: * @param precision the date precision michael@0: * @param tz the timezone michael@0: * @see Dates#PRECISION_DAY michael@0: * @see Dates#PRECISION_SECOND michael@0: */ michael@0: protected Date(final long time, final int precision, TimeZone tz) { michael@3: super(time, DEFAULT_PATTERN, precision, tz); michael@0: } michael@0: michael@0: /** michael@0: * @param date a date value michael@0: */ michael@0: public Date(final java.util.Date date) { michael@0: // this(); michael@0: this(date.getTime(), Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); michael@0: // setTime(date.getTime()); michael@0: } michael@0: michael@0: /** michael@0: * @param value a string representation of a date michael@0: * @throws ParseException where the specified string is not a valid date michael@0: */ michael@0: public Date(final String value) throws ParseException { michael@0: this(); michael@3: try { michael@3: setTime(getFormat().parse(value).getTime()); michael@3: } catch (ParseException pe) { michael@3: if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_VCARD_COMPATIBILITY)) { michael@3: final DateFormat parseFormat = new SimpleDateFormat(VCARD_PATTERN); michael@3: parseFormat.setTimeZone(TimeZones.getDateTimeZone()); michael@3: setTime(parseFormat.parse(value).getTime()); michael@3: } michael@3: else { michael@3: throw pe; michael@3: } michael@3: } michael@0: } michael@0: michael@0: /** michael@0: * @param value a string representation of a date michael@0: * @param pattern a date pattern to apply when parsing michael@0: * @throws ParseException where the specified string is not a valid date michael@0: */ michael@0: public Date(String value, String pattern) throws ParseException { michael@3: super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); michael@3: final DateFormat parseFormat = new SimpleDateFormat(pattern); michael@3: parseFormat.setTimeZone(TimeZones.getDateTimeZone()); michael@3: setTime(parseFormat.parse(value).getTime()); michael@0: } michael@0: }