29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
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. |
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 */ |
31 */ |
32 package net.fortuna.ical4j.model; |
32 package net.fortuna.ical4j.model; |
33 |
33 |
|
34 import java.text.DateFormat; |
34 import java.text.ParseException; |
35 import java.text.ParseException; |
|
36 import java.text.SimpleDateFormat; |
35 import java.util.TimeZone; |
37 import java.util.TimeZone; |
36 |
38 |
|
39 import net.fortuna.ical4j.util.CompatibilityHints; |
37 import net.fortuna.ical4j.util.Dates; |
40 import net.fortuna.ical4j.util.Dates; |
38 import net.fortuna.ical4j.util.TimeZones; |
41 import net.fortuna.ical4j.util.TimeZones; |
39 |
42 |
40 |
43 |
41 /** |
44 /** |
85 */ |
88 */ |
86 public class Date extends Iso8601 { |
89 public class Date extends Iso8601 { |
87 |
90 |
88 private static final long serialVersionUID = 7136072363141363141L; |
91 private static final long serialVersionUID = 7136072363141363141L; |
89 |
92 |
90 private static final String PATTERN = "yyyyMMdd"; |
93 private static final String DEFAULT_PATTERN = "yyyyMMdd"; |
|
94 |
|
95 private static final String VCARD_PATTERN = "yyyy'-'MM'-'dd"; |
91 |
96 |
92 /** |
97 /** |
93 * Default constructor. |
98 * Default constructor. |
94 */ |
99 */ |
95 public Date() { |
100 public Date() { |
96 super(PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
101 super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
97 } |
102 } |
98 |
103 |
99 /** |
104 /** |
100 * Creates a new date instance with the specified precision. This |
105 * Creates a new date instance with the specified precision. This |
101 * constructor is only intended for use by sub-classes. |
106 * constructor is only intended for use by sub-classes. |
103 * @param tz the timezone |
108 * @param tz the timezone |
104 * @see Dates#PRECISION_DAY |
109 * @see Dates#PRECISION_DAY |
105 * @see Dates#PRECISION_SECOND |
110 * @see Dates#PRECISION_SECOND |
106 */ |
111 */ |
107 protected Date(final int precision, TimeZone tz) { |
112 protected Date(final int precision, TimeZone tz) { |
108 super(PATTERN, precision, tz); |
113 super(DEFAULT_PATTERN, precision, tz); |
109 } |
114 } |
110 |
115 |
111 /** |
116 /** |
112 * @param time a date value in milliseconds |
117 * @param time a date value in milliseconds |
113 */ |
118 */ |
114 public Date(final long time) { |
119 public Date(final long time) { |
115 super(time, PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
120 super(time, DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
116 } |
121 } |
117 |
122 |
118 /** |
123 /** |
119 * Creates a new date instance with the specified precision. This |
124 * Creates a new date instance with the specified precision. This |
120 * constructor is only intended for use by sub-classes. |
125 * constructor is only intended for use by sub-classes. |
123 * @param tz the timezone |
128 * @param tz the timezone |
124 * @see Dates#PRECISION_DAY |
129 * @see Dates#PRECISION_DAY |
125 * @see Dates#PRECISION_SECOND |
130 * @see Dates#PRECISION_SECOND |
126 */ |
131 */ |
127 protected Date(final long time, final int precision, TimeZone tz) { |
132 protected Date(final long time, final int precision, TimeZone tz) { |
128 super(time, PATTERN, precision, tz); |
133 super(time, DEFAULT_PATTERN, precision, tz); |
129 } |
134 } |
130 |
135 |
131 /** |
136 /** |
132 * @param date a date value |
137 * @param date a date value |
133 */ |
138 */ |
141 * @param value a string representation of a date |
146 * @param value a string representation of a date |
142 * @throws ParseException where the specified string is not a valid date |
147 * @throws ParseException where the specified string is not a valid date |
143 */ |
148 */ |
144 public Date(final String value) throws ParseException { |
149 public Date(final String value) throws ParseException { |
145 this(); |
150 this(); |
146 setTime(getFormat().parse(value).getTime()); |
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 } |
147 } |
163 } |
148 |
164 |
149 /** |
165 /** |
150 * @param value a string representation of a date |
166 * @param value a string representation of a date |
151 * @param pattern a date pattern to apply when parsing |
167 * @param pattern a date pattern to apply when parsing |
152 * @throws ParseException where the specified string is not a valid date |
168 * @throws ParseException where the specified string is not a valid date |
153 */ |
169 */ |
154 public Date(String value, String pattern) throws ParseException { |
170 public Date(String value, String pattern) throws ParseException { |
155 super(pattern, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
171 super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); |
156 setTime(getFormat().parse(value).getTime()); |
172 final DateFormat parseFormat = new SimpleDateFormat(pattern); |
|
173 parseFormat.setTimeZone(TimeZones.getDateTimeZone()); |
|
174 setTime(parseFormat.parse(value).getTime()); |
157 } |
175 } |
158 } |
176 } |