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

changeset 0
fb9019fb1bf7
child 3
73bdfa70b04e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/Date.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,158 @@
     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.model;
    1.36 +
    1.37 +import java.text.ParseException;
    1.38 +import java.util.TimeZone;
    1.39 +
    1.40 +import net.fortuna.ical4j.util.Dates;
    1.41 +import net.fortuna.ical4j.util.TimeZones;
    1.42 +
    1.43 +
    1.44 +/**
    1.45 + * $Id$
    1.46 + *
    1.47 + * Created on 26/06/2005
    1.48 + *
    1.49 + * Base class for all representations of time values in RFC2445.
    1.50 + *
    1.51 + * <pre>
    1.52 + * 4.3.4 Date
    1.53 + * 
    1.54 + *    Value Name: DATE
    1.55 + * 
    1.56 + *    Purpose: This value type is used to identify values that contain a
    1.57 + *    calendar date.
    1.58 + * 
    1.59 + *    Formal Definition: The value type is defined by the following
    1.60 + *    notation:
    1.61 + * 
    1.62 + *      date               = date-value
    1.63 + * 
    1.64 + *      date-value         = date-fullyear date-month date-mday
    1.65 + *      date-fullyear      = 4DIGIT
    1.66 + *      date-month         = 2DIGIT        ;01-12
    1.67 + *      date-mday          = 2DIGIT        ;01-28, 01-29, 01-30, 01-31
    1.68 + *                                         ;based on month/year
    1.69 + * 
    1.70 + *    Description: If the property permits, multiple "date" values are
    1.71 + *    specified as a COMMA character (US-ASCII decimal 44) separated list
    1.72 + *    of values. The format for the value type is expressed as the [ISO
    1.73 + *    8601] complete representation, basic format for a calendar date. The
    1.74 + *    textual format specifies a four-digit year, two-digit month, and
    1.75 + *    two-digit day of the month. There are no separator characters between
    1.76 + *    the year, month and day component text.
    1.77 + * 
    1.78 + *    No additional content value encoding (i.e., BACKSLASH character
    1.79 + *    encoding) is defined for this value type.
    1.80 + * 
    1.81 + *    Example: The following represents July 14, 1997:
    1.82 + * 
    1.83 + *      19970714
    1.84 + * 
    1.85 + * </pre>
    1.86 + * 
    1.87 + * @author Ben Fortuna
    1.88 + */
    1.89 +public class Date extends Iso8601 {
    1.90 +
    1.91 +    private static final long serialVersionUID = 7136072363141363141L;
    1.92 +
    1.93 +    private static final String PATTERN = "yyyyMMdd";
    1.94 +
    1.95 +    /**
    1.96 +     * Default constructor.
    1.97 +     */
    1.98 +    public Date() {
    1.99 +        super(PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
   1.100 +    }
   1.101 +    
   1.102 +    /**
   1.103 +     * Creates a new date instance with the specified precision. This
   1.104 +     * constructor is only intended for use by sub-classes.
   1.105 +     * @param precision the date precision
   1.106 +     * @param tz the timezone
   1.107 +     * @see Dates#PRECISION_DAY
   1.108 +     * @see Dates#PRECISION_SECOND
   1.109 +     */
   1.110 +    protected Date(final int precision, TimeZone tz) {
   1.111 +        super(PATTERN, precision, tz);
   1.112 +    }
   1.113 +
   1.114 +    /**
   1.115 +     * @param time a date value in milliseconds
   1.116 +     */
   1.117 +    public Date(final long time) {
   1.118 +        super(time, PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
   1.119 +    }
   1.120 +    
   1.121 +    /**
   1.122 +     * Creates a new date instance with the specified precision. This
   1.123 +     * constructor is only intended for use by sub-classes.
   1.124 +     * @param time a date value in milliseconds
   1.125 +     * @param precision the date precision
   1.126 +     * @param tz the timezone
   1.127 +     * @see Dates#PRECISION_DAY
   1.128 +     * @see Dates#PRECISION_SECOND
   1.129 +     */
   1.130 +    protected Date(final long time, final int precision, TimeZone tz) {
   1.131 +        super(time, PATTERN, precision, tz);
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * @param date a date value
   1.136 +     */
   1.137 +    public Date(final java.util.Date date) {
   1.138 +//        this();
   1.139 +        this(date.getTime(), Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
   1.140 +//        setTime(date.getTime());
   1.141 +    }
   1.142 +
   1.143 +    /**
   1.144 +     * @param value a string representation of a date
   1.145 +     * @throws ParseException where the specified string is not a valid date
   1.146 +     */
   1.147 +    public Date(final String value) throws ParseException {
   1.148 +        this();
   1.149 +        setTime(getFormat().parse(value).getTime());
   1.150 +    }
   1.151 +    
   1.152 +    /**
   1.153 +     * @param value a string representation of a date
   1.154 +     * @param pattern a date pattern to apply when parsing
   1.155 +     * @throws ParseException where the specified string is not a valid date
   1.156 +     */
   1.157 +    public Date(String value, String pattern) throws ParseException {
   1.158 +        super(pattern, Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
   1.159 +        setTime(getFormat().parse(value).getTime());
   1.160 +    }
   1.161 +}

mercurial