/
TemporalAdapter.java
281 lines (248 loc) · 11 KB
/
TemporalAdapter.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package net.fortuna.ical4j.model;
import net.fortuna.ical4j.model.parameter.TzId;
import java.io.Serializable;
import java.time.*;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.Objects;
/**
* The iCalendar specification supports multiple representations of date/time values, as outlined
* below. This class encapsulates a {@link Temporal} value
* and provides support for all corresponding representations in the specification.
*
* The recommended {@link Temporal} implementations for use with iCal4j are as follows:
*
* <ul>
* <li>{@link LocalDate} - represents an iCalendar DATE value as defined in section 3.3.4 of RFC5545</li>
* <li>{@link LocalDateTime} - represents an iCalendar FORM #1: DATE-TIME value as defined in section 3.3.5 of RFC5545</li>
* <li>{@link java.time.Instant} - represents an iCalendar FORM #2: DATE-TIME value as defined in section 3.3.5 of RFC5545</li>
* <li>{@link ZonedDateTime} - represents an iCalendar FORM #3: DATE-TIME value as defined in section 3.3.5 of RFC5545</li>
* </ul>
*
* Note that a local (i.e. floating) temporal type is used we need to apply a {@link ZoneId} for calculations such as
* recurrence inclusions and other date-based comparisons. Use {@link TemporalAdapter#isFloating(Temporal)} to determine floating
* instances.
*
* @param <T> A concrete implementation of {@link Temporal}
*/
public class TemporalAdapter<T extends Temporal> implements Serializable {
/**
* A formatter capable of parsing to multiple temporal types based on the input string.
*/
private static CalendarDateFormat PARSE_FORMAT = new CalendarDateFormat(
"yyyyMMdd['T'HHmmss[X]]", Instant::from, LocalDateTime::from, LocalDate::from);
private final String valueString;
private final TzId tzId;
private transient TimeZoneRegistry timeZoneRegistry;
private transient T temporal;
public TemporalAdapter(TemporalAdapter<T> adapter) {
this.temporal = adapter.temporal;
this.valueString = adapter.valueString;
this.tzId = adapter.tzId;
this.timeZoneRegistry = adapter.timeZoneRegistry;
}
public TemporalAdapter(T temporal) {
this(temporal, null);
}
public TemporalAdapter(T temporal, TimeZoneRegistry timeZoneRegistry) {
Objects.requireNonNull(temporal, "temporal");
this.temporal = temporal;
this.valueString = toString(temporal, ZoneId.systemDefault());
if (ChronoUnit.SECONDS.isSupportedBy(temporal) && !isFloating(temporal) && !isUtc(temporal)) {
this.tzId = new TzId.Factory().createParameter(ZoneId.systemDefault().getId());
} else {
this.tzId = null;
}
this.timeZoneRegistry = timeZoneRegistry;
}
private TemporalAdapter(String valueString) {
this(valueString, null);
}
/**
* Support lazy parsing of value string using a zone id to allow full initialisation of
* {@link java.time.zone.ZoneRulesProvider} instances.
*
* @param value a string representation of a floating date/time value
* @param tzId a zone id to apply to the parsed value
*/
private TemporalAdapter(String value, TzId tzId) {
this(value, tzId, null);
}
/**
*
* @param value a string representation of a floating date/time value
* @param tzId a zone id to apply to the parsed value
* @param timeZoneRegistry timezone definitions
*/
private TemporalAdapter(String value, TzId tzId, TimeZoneRegistry timeZoneRegistry) {
this.valueString = value;
this.tzId = tzId;
this.timeZoneRegistry = timeZoneRegistry;
}
public T getTemporal() {
if (temporal == null) {
synchronized (valueString) {
if (temporal == null) {
if (tzId != null) {
temporal = (T) CalendarDateFormat.FLOATING_DATE_TIME_FORMAT.parse(valueString,
tzId.toZoneId(timeZoneRegistry));
} else {
temporal = (T) PARSE_FORMAT.parse(valueString);
}
/*
temporal = (T) Proxy.newProxyInstance(ChronoZonedDateTime.class.getClassLoader(),
new Class[]{ChronoZonedDateTime.class},
new InvocationHandler() {
private ChronoZonedDateTime<LocalDate> temporal;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (temporal == null) {
temporal = CalendarDateFormat.FLOATING_DATE_TIME_FORMAT.parse(valueString, tzId.toZoneId());
}
return method.invoke(temporal, args);
}
});
*/
}
}
}
return temporal;
}
@Override
public String toString() {
return toString(getTemporal(), ZoneId.systemDefault());
}
public String toString(ZoneId zoneId) {
return toString(getTemporal(), zoneId);
}
private String toString(T temporal, ZoneId zoneId) {
if (!ChronoUnit.SECONDS.isSupportedBy(temporal)) {
return toString(CalendarDateFormat.DATE_FORMAT, temporal);
} else {
if (isFloating(getTemporal())) {
return toString(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT, temporal);
} else if (isUtc(getTemporal())) {
return toString(CalendarDateFormat.UTC_DATE_TIME_FORMAT, temporal);
} else {
return toString(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT, zoneId, temporal);
}
}
}
private String toString(CalendarDateFormat format, T temporal) {
return format.format(temporal);
}
private String toString(CalendarDateFormat format, ZoneId zoneId, T temporal) {
return format.format(temporal, zoneId);
}
public ZonedDateTime toLocalTime() {
return toLocalTime(ZoneId.systemDefault());
}
public ZonedDateTime toLocalTime(ZoneId zoneId) {
if (isFloating(getTemporal())) {
return ((LocalDate) getTemporal()).atStartOfDay().atZone(zoneId);
} else if (isUtc(getTemporal())) {
return ((Instant) getTemporal()).atZone(zoneId);
} else {
return ZonedDateTime.from(getTemporal());
}
}
/**
* Parse a string representation of a temporal value.
*
* @param value a string representing a temporal
* @return an adapter containing the parsed temporal value and format type
* @throws DateTimeParseException if the string cannot be parsed
*/
public static <T extends Temporal> TemporalAdapter<T> parse(String value) throws DateTimeParseException {
return new TemporalAdapter<>((T) PARSE_FORMAT.parse(value));
}
/**
* Parse a string representation of a temporal value applicable to the specified timezone.
*
* @param value a string representing a floating temporal value
* @param zoneId a timezone applied to the parsed value
* @return an adapter containing the parsed temporal value
* @throws DateTimeParseException if the string cannot be parsed
*/
public static TemporalAdapter<ZonedDateTime> parse(String value, ZoneId zoneId) {
return new TemporalAdapter<>(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT.parse(value, zoneId));
}
/**
* Parse a string representation of a temporal value applicable to the specified timezone.
*
* @param value a string representing a floating temporal value
* @param tzId a timezone applied to the parsed value
* @return an adapter containing the parsed temporal value
* @throws DateTimeParseException if the string cannot be parsed
*/
public static TemporalAdapter<ZonedDateTime> parse(String value, TzId tzId) {
return new TemporalAdapter<>(value, tzId);
}
/**
*
* @param value a string representing a floating temporal value
* @param tzId a timezone applied to the parsed value
* @param timeZoneRegistry timezone definitions
* @return
*/
public static TemporalAdapter<ZonedDateTime> parse(String value, TzId tzId, TimeZoneRegistry timeZoneRegistry) {
return new TemporalAdapter<>(value, tzId, timeZoneRegistry);
}
/**
* This method provides support for conversion of legacy {@link Date} and {@link DateTime} instances to temporal
* values.
*
* @param date a date/time instance
* @return a temporal adapter instance equivalent to the specified date/time value
*/
@SuppressWarnings("deprecation")
public static TemporalAdapter from(Date date) {
Temporal temporal;
if (date instanceof DateTime) {
DateTime dateTime = (DateTime) date;
if (dateTime.isUtc()) {
temporal = date.toInstant();
} else if (dateTime.getTimeZone() == null) {
temporal = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
} else {
temporal = ZonedDateTime.ofInstant(date.toInstant(), dateTime.getTimeZone().toZoneId());
}
} else {
temporal = LocalDate.from(date.toInstant());
}
return new TemporalAdapter<>(temporal);
}
/**
* Indicates whether the temporal type represents a "floating" date/time value.
* @return true if the temporal type is floating, otherwise false
*/
public static boolean isFloating(Temporal date) {
return !ChronoField.OFFSET_SECONDS.isSupportedBy(date) &&
!ChronoField.INSTANT_SECONDS.isSupportedBy(date);
}
/**
* Indicates whether the temporal type represents a UTC date/time value.
* @return true if the temporal type is in UTC time, otherwise false
*/
public static boolean isUtc(Temporal date) {
return !ChronoField.OFFSET_SECONDS.isSupportedBy(date);
}
public static <T extends Temporal> boolean isBefore(T date1, T date2) {
if (date1 instanceof LocalDate && date2 instanceof LocalDate) {
return ((LocalDate) date1).isBefore((LocalDate) date2);
} else if (date1 instanceof LocalDateTime && date2 instanceof LocalDateTime) {
return ((LocalDateTime) date1).isBefore((LocalDateTime) date2);
}
return Instant.from(date1).isBefore(Instant.from(date2));
}
public static <T extends Temporal> boolean isAfter(T date1, T date2) {
if (date1 instanceof LocalDate) {
return ((LocalDate) date1).isAfter((LocalDate) date2);
} else if (date1 instanceof LocalDateTime && date2 instanceof LocalDateTime) {
return ((LocalDateTime) date1).isAfter((LocalDateTime) date2);
}
return Instant.from(date1).isAfter(Instant.from(date2));
}
}