Tue, 10 Feb 2015 19:58:00 +0100
Upgrade the upgraded ical4j component to use org.apache.commons.lang3.
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;
34 import java.text.DateFormat;
35 import java.text.ParseException;
36 import java.text.SimpleDateFormat;
37 import java.util.TimeZone;
39 import net.fortuna.ical4j.util.CompatibilityHints;
40 import net.fortuna.ical4j.util.Dates;
41 import net.fortuna.ical4j.util.TimeZones;
44 /**
45 * $Id$
46 *
47 * Created on 26/06/2005
48 *
49 * Base class for all representations of time values in RFC2445.
50 *
51 * <pre>
52 * 4.3.4 Date
53 *
54 * Value Name: DATE
55 *
56 * Purpose: This value type is used to identify values that contain a
57 * calendar date.
58 *
59 * Formal Definition: The value type is defined by the following
60 * notation:
61 *
62 * date = date-value
63 *
64 * date-value = date-fullyear date-month date-mday
65 * date-fullyear = 4DIGIT
66 * date-month = 2DIGIT ;01-12
67 * date-mday = 2DIGIT ;01-28, 01-29, 01-30, 01-31
68 * ;based on month/year
69 *
70 * Description: If the property permits, multiple "date" values are
71 * specified as a COMMA character (US-ASCII decimal 44) separated list
72 * of values. The format for the value type is expressed as the [ISO
73 * 8601] complete representation, basic format for a calendar date. The
74 * textual format specifies a four-digit year, two-digit month, and
75 * two-digit day of the month. There are no separator characters between
76 * the year, month and day component text.
77 *
78 * No additional content value encoding (i.e., BACKSLASH character
79 * encoding) is defined for this value type.
80 *
81 * Example: The following represents July 14, 1997:
82 *
83 * 19970714
84 *
85 * </pre>
86 *
87 * @author Ben Fortuna
88 */
89 public class Date extends Iso8601 {
91 private static final long serialVersionUID = 7136072363141363141L;
93 private static final String DEFAULT_PATTERN = "yyyyMMdd";
95 private static final String VCARD_PATTERN = "yyyy'-'MM'-'dd";
97 /**
98 * Default constructor.
99 */
100 public Date() {
101 super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
102 }
104 /**
105 * Creates a new date instance with the specified precision. This
106 * constructor is only intended for use by sub-classes.
107 * @param precision the date precision
108 * @param tz the timezone
109 * @see Dates#PRECISION_DAY
110 * @see Dates#PRECISION_SECOND
111 */
112 protected Date(final int precision, TimeZone tz) {
113 super(DEFAULT_PATTERN, precision, tz);
114 }
116 /**
117 * @param time a date value in milliseconds
118 */
119 public Date(final long time) {
120 super(time, DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
121 }
123 /**
124 * Creates a new date instance with the specified precision. This
125 * constructor is only intended for use by sub-classes.
126 * @param time a date value in milliseconds
127 * @param precision the date precision
128 * @param tz the timezone
129 * @see Dates#PRECISION_DAY
130 * @see Dates#PRECISION_SECOND
131 */
132 protected Date(final long time, final int precision, TimeZone tz) {
133 super(time, DEFAULT_PATTERN, precision, tz);
134 }
136 /**
137 * @param date a date value
138 */
139 public Date(final java.util.Date date) {
140 // this();
141 this(date.getTime(), Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
142 // setTime(date.getTime());
143 }
145 /**
146 * @param value a string representation of a date
147 * @throws ParseException where the specified string is not a valid date
148 */
149 public Date(final String value) throws ParseException {
150 this();
151 try {
152 setTime(getFormat().parse(value).getTime());
153 } catch (ParseException pe) {
154 if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_VCARD_COMPATIBILITY)) {
155 final DateFormat parseFormat = new SimpleDateFormat(VCARD_PATTERN);
156 parseFormat.setTimeZone(TimeZones.getDateTimeZone());
157 setTime(parseFormat.parse(value).getTime());
158 }
159 else {
160 throw pe;
161 }
162 }
163 }
165 /**
166 * @param value a string representation of a date
167 * @param pattern a date pattern to apply when parsing
168 * @throws ParseException where the specified string is not a valid date
169 */
170 public Date(String value, String pattern) throws ParseException {
171 super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone());
172 final DateFormat parseFormat = new SimpleDateFormat(pattern);
173 parseFormat.setTimeZone(TimeZones.getDateTimeZone());
174 setTime(parseFormat.parse(value).getTime());
175 }
176 }