1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package net.fortuna.ical4j.model.property;
35
36 import java.text.ParseException;
37 import java.util.Date;
38
39 import net.fortuna.ical4j.model.Parameter;
40 import net.fortuna.ical4j.model.ParameterList;
41 import net.fortuna.ical4j.model.Period;
42 import net.fortuna.ical4j.model.Property;
43 import net.fortuna.ical4j.model.ValidationException;
44 import net.fortuna.ical4j.model.parameter.Value;
45 import net.fortuna.ical4j.util.DateFormat;
46 import net.fortuna.ical4j.util.DateTimeFormat;
47 import net.fortuna.ical4j.util.ParameterValidator;
48
49 /***
50 * Defines an RDATE iCalendar component property.
51 *
52 * @author benf
53 */
54 public class RDate extends Property {
55
56 private Date time;
57
58 private Period period;
59
60
61
62 private boolean utc = true;
63
64 /***
65 * @param aList
66 * a list of parameters for this component
67 * @param aValue
68 * a value string for this component
69 * @throws ParseException
70 * where the specified value string is not a valid
71 * date-time/date representation
72 */
73 public RDate(final ParameterList aList, final String aValue)
74 throws ParseException {
75 super(RDATE, aList);
76
77
78 Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
79
80 if (valueParam != null && Value.DATE.equals(valueParam.getValue())) {
81 time = DateFormat.getInstance().parse(aValue);
82 }
83 else if (valueParam != null && Value.PERIOD.equals(valueParam)) {
84 period = new Period(aValue);
85 }
86 else {
87 time = DateTimeFormat.getInstance().parse(aValue);
88 }
89 }
90
91 /***
92 * Constructor. Date or Date-Time format is determined based on the presence
93 * of a VALUE parameter.
94 *
95 * @param aList
96 * a list of parameters for this component
97 * @param aDate
98 * a date representation of a date or date-time
99 */
100 public RDate(final ParameterList aList, final Date aDate) {
101 super(RDATE, aList);
102
103 time = aDate;
104 }
105
106 /***
107 * Constructor.
108 *
109 * @param aList
110 * a list of parameters for this component
111 * @param aPeriod
112 * a period
113 */
114 public RDate(final ParameterList aList, final Period aPeriod) {
115 super(RDATE, aList);
116
117 period = aPeriod;
118 }
119
120 /***
121 * @see net.fortuna.ical4j.model.Property#validate()
122 */
123 public final void validate() throws ValidationException {
124
125
126
127
128
129
130 ParameterValidator.getInstance().validateOneOrLess(Parameter.VALUE,
131 getParameters());
132
133 Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
134
135 if (valueParam != null
136 && !Value.DATE_TIME.equals(valueParam.getValue())
137 && !Value.DATE.equals(valueParam.getValue())) { throw new ValidationException(
138 "Parameter [" + Parameter.VALUE + "] is invalid"); }
139
140 ParameterValidator.getInstance().validateOneOrLess(Parameter.TZID,
141 getParameters());
142
143
144
145
146
147
148 }
149
150 /***
151 * @return Returns the period.
152 */
153 public final Period getPeriod() {
154 return period;
155 }
156
157 /***
158 * @return Returns the time.
159 */
160 public final Date getTime() {
161 return time;
162 }
163
164
165
166
167
168
169 public String getValue() {
170 if (getTime() != null) {
171
172 Parameter valueParam = getParameters()
173 .getParameter(Parameter.VALUE);
174
175 if (valueParam != null && Value.DATE.equals(valueParam)) {
176 return DateFormat.getInstance().format(getTime());
177 }
178 else {
179 return DateTimeFormat.getInstance().format(getTime(), isUtc());
180 }
181 }
182 else if (getPeriod() != null) { return getPeriod().toString(); }
183
184 return null;
185 }
186
187 /***
188 * @return Returns the utc.
189 */
190 public boolean isUtc() {
191 return utc;
192 }
193
194 /***
195 * @param utc
196 * The utc to set.
197 */
198 public void setUtc(boolean utc) {
199 this.utc = utc;
200 }
201 }