1.1 --- a/src/net/fortuna/ical4j/model/Date.java Tue Feb 10 19:25:00 2015 +0100 1.2 +++ b/src/net/fortuna/ical4j/model/Date.java Tue Feb 10 19:38:00 2015 +0100 1.3 @@ -31,9 +31,12 @@ 1.4 */ 1.5 package net.fortuna.ical4j.model; 1.6 1.7 +import java.text.DateFormat; 1.8 import java.text.ParseException; 1.9 +import java.text.SimpleDateFormat; 1.10 import java.util.TimeZone; 1.11 1.12 +import net.fortuna.ical4j.util.CompatibilityHints; 1.13 import net.fortuna.ical4j.util.Dates; 1.14 import net.fortuna.ical4j.util.TimeZones; 1.15 1.16 @@ -87,13 +90,15 @@ 1.17 1.18 private static final long serialVersionUID = 7136072363141363141L; 1.19 1.20 - private static final String PATTERN = "yyyyMMdd"; 1.21 + private static final String DEFAULT_PATTERN = "yyyyMMdd"; 1.22 + 1.23 + private static final String VCARD_PATTERN = "yyyy'-'MM'-'dd"; 1.24 1.25 /** 1.26 * Default constructor. 1.27 */ 1.28 public Date() { 1.29 - super(PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); 1.30 + super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); 1.31 } 1.32 1.33 /** 1.34 @@ -105,14 +110,14 @@ 1.35 * @see Dates#PRECISION_SECOND 1.36 */ 1.37 protected Date(final int precision, TimeZone tz) { 1.38 - super(PATTERN, precision, tz); 1.39 + super(DEFAULT_PATTERN, precision, tz); 1.40 } 1.41 1.42 /** 1.43 * @param time a date value in milliseconds 1.44 */ 1.45 public Date(final long time) { 1.46 - super(time, PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); 1.47 + super(time, DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); 1.48 } 1.49 1.50 /** 1.51 @@ -125,7 +130,7 @@ 1.52 * @see Dates#PRECISION_SECOND 1.53 */ 1.54 protected Date(final long time, final int precision, TimeZone tz) { 1.55 - super(time, PATTERN, precision, tz); 1.56 + super(time, DEFAULT_PATTERN, precision, tz); 1.57 } 1.58 1.59 /** 1.60 @@ -143,7 +148,18 @@ 1.61 */ 1.62 public Date(final String value) throws ParseException { 1.63 this(); 1.64 - setTime(getFormat().parse(value).getTime()); 1.65 + try { 1.66 + setTime(getFormat().parse(value).getTime()); 1.67 + } catch (ParseException pe) { 1.68 + if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_VCARD_COMPATIBILITY)) { 1.69 + final DateFormat parseFormat = new SimpleDateFormat(VCARD_PATTERN); 1.70 + parseFormat.setTimeZone(TimeZones.getDateTimeZone()); 1.71 + setTime(parseFormat.parse(value).getTime()); 1.72 + } 1.73 + else { 1.74 + throw pe; 1.75 + } 1.76 + } 1.77 } 1.78 1.79 /** 1.80 @@ -152,7 +168,9 @@ 1.81 * @throws ParseException where the specified string is not a valid date 1.82 */ 1.83 public Date(String value, String pattern) throws ParseException { 1.84 - super(pattern, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); 1.85 - setTime(getFormat().parse(value).getTime()); 1.86 + super(DEFAULT_PATTERN, Dates.PRECISION_DAY, TimeZones.getDateTimeZone()); 1.87 + final DateFormat parseFormat = new SimpleDateFormat(pattern); 1.88 + parseFormat.setTimeZone(TimeZones.getDateTimeZone()); 1.89 + setTime(parseFormat.parse(value).getTime()); 1.90 } 1.91 }