|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import java.text.ParseException; |
|
35 import java.util.TimeZone; |
|
36 |
|
37 import net.fortuna.ical4j.util.Dates; |
|
38 import net.fortuna.ical4j.util.TimeZones; |
|
39 |
|
40 |
|
41 /** |
|
42 * $Id$ |
|
43 * |
|
44 * Created on 26/06/2005 |
|
45 * |
|
46 * Base class for all representations of time values in RFC2445. |
|
47 * |
|
48 * <pre> |
|
49 * 4.3.4 Date |
|
50 * |
|
51 * Value Name: DATE |
|
52 * |
|
53 * Purpose: This value type is used to identify values that contain a |
|
54 * calendar date. |
|
55 * |
|
56 * Formal Definition: The value type is defined by the following |
|
57 * notation: |
|
58 * |
|
59 * date = date-value |
|
60 * |
|
61 * date-value = date-fullyear date-month date-mday |
|
62 * date-fullyear = 4DIGIT |
|
63 * date-month = 2DIGIT ;01-12 |
|
64 * date-mday = 2DIGIT ;01-28, 01-29, 01-30, 01-31 |
|
65 * ;based on month/year |
|
66 * |
|
67 * Description: If the property permits, multiple "date" values are |
|
68 * specified as a COMMA character (US-ASCII decimal 44) separated list |
|
69 * of values. The format for the value type is expressed as the [ISO |
|
70 * 8601] complete representation, basic format for a calendar date. The |
|
71 * textual format specifies a four-digit year, two-digit month, and |
|
72 * two-digit day of the month. There are no separator characters between |
|
73 * the year, month and day component text. |
|
74 * |
|
75 * No additional content value encoding (i.e., BACKSLASH character |
|
76 * encoding) is defined for this value type. |
|
77 * |
|
78 * Example: The following represents July 14, 1997: |
|
79 * |
|
80 * 19970714 |
|
81 * |
|
82 * </pre> |
|
83 * |
|
84 * @author Ben Fortuna |
|
85 */ |
|
86 public class Date extends Iso8601 { |
|
87 |
|
88 private static final long serialVersionUID = 7136072363141363141L; |
|
89 |
|
90 private static final String PATTERN = "yyyyMMdd"; |
|
91 |
|
92 /** |
|
93 * Default constructor. |
|
94 */ |
|
95 public Date() { |
|
96 super(PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
|
97 } |
|
98 |
|
99 /** |
|
100 * Creates a new date instance with the specified precision. This |
|
101 * constructor is only intended for use by sub-classes. |
|
102 * @param precision the date precision |
|
103 * @param tz the timezone |
|
104 * @see Dates#PRECISION_DAY |
|
105 * @see Dates#PRECISION_SECOND |
|
106 */ |
|
107 protected Date(final int precision, TimeZone tz) { |
|
108 super(PATTERN, precision, tz); |
|
109 } |
|
110 |
|
111 /** |
|
112 * @param time a date value in milliseconds |
|
113 */ |
|
114 public Date(final long time) { |
|
115 super(time, PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
|
116 } |
|
117 |
|
118 /** |
|
119 * Creates a new date instance with the specified precision. This |
|
120 * constructor is only intended for use by sub-classes. |
|
121 * @param time a date value in milliseconds |
|
122 * @param precision the date precision |
|
123 * @param tz the timezone |
|
124 * @see Dates#PRECISION_DAY |
|
125 * @see Dates#PRECISION_SECOND |
|
126 */ |
|
127 protected Date(final long time, final int precision, TimeZone tz) { |
|
128 super(time, PATTERN, precision, tz); |
|
129 } |
|
130 |
|
131 /** |
|
132 * @param date a date value |
|
133 */ |
|
134 public Date(final java.util.Date date) { |
|
135 // this(); |
|
136 this(date.getTime(), Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
|
137 // setTime(date.getTime()); |
|
138 } |
|
139 |
|
140 /** |
|
141 * @param value a string representation of a date |
|
142 * @throws ParseException where the specified string is not a valid date |
|
143 */ |
|
144 public Date(final String value) throws ParseException { |
|
145 this(); |
|
146 setTime(getFormat().parse(value).getTime()); |
|
147 } |
|
148 |
|
149 /** |
|
150 * @param value a string representation of a date |
|
151 * @param pattern a date pattern to apply when parsing |
|
152 * @throws ParseException where the specified string is not a valid date |
|
153 */ |
|
154 public Date(String value, String pattern) throws ParseException { |
|
155 super(pattern, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
|
156 setTime(getFormat().parse(value).getTime()); |
|
157 } |
|
158 } |