Skip to content

Commit

Permalink
Date changes
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 13, 2020
1 parent 96d5f38 commit 1d83736
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Expand Up @@ -6,6 +6,7 @@
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -225,6 +226,13 @@ public String format(TemporalAccessor date, ZoneId zoneId) {
return getFormatter().withZone(zoneId).format(date);
}

public static CalendarDateFormat from(List<? extends Temporal> list) {
if (!list.isEmpty()) {
return from(list.get(0));
}
return FLOATING_DATE_TIME_FORMAT;
}

public static CalendarDateFormat from(Temporal temporal) {
if (temporal instanceof Instant) {
return UTC_DATE_TIME_FORMAT;
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/DateList.java
Expand Up @@ -88,7 +88,7 @@ public DateList(final boolean unmodifiable, CalendarDateFormat dateFormat) {
* @param list a list of dates to include in the new list
*/
public DateList(final List<T> list) {
this(list, CalendarDateFormat.FLOATING_DATE_TIME_FORMAT);
this(list, CalendarDateFormat.from(list));
}

public DateList(final List<T> list, CalendarDateFormat dateFormat) {
Expand All @@ -106,10 +106,15 @@ public DateList(final List<T> list, CalendarDateFormat dateFormat) {
* @throws DateTimeParseException
*/
public static <T extends Temporal> DateList<T> parse(String value) {
return parse(value, null);
}

public static <T extends Temporal> DateList<T> parse(String value, CalendarDateFormat calendarDateFormat) {
List<Temporal> dates = Arrays.stream(value.split(",")).map(TemporalAdapter::parse)
.map(TemporalAdapter::getTemporal).collect(Collectors.toList());
if (!dates.isEmpty()) {
return new DateList<>((List<T>) dates, CalendarDateFormat.from(dates.get(0)));

if (calendarDateFormat != null) {
return new DateList<>((List<T>) dates, calendarDateFormat);
} else {
return new DateList<>((List<T>) dates);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/TemporalAdapter.java
Expand Up @@ -166,7 +166,11 @@ public ZonedDateTime toLocalTime() {

public ZonedDateTime toLocalTime(ZoneId zoneId) {
if (isFloating(getTemporal())) {
return ((LocalDate) getTemporal()).atStartOfDay().atZone(zoneId);
if (getTemporal() instanceof LocalDateTime) {
return ((LocalDateTime) getTemporal()).atZone(zoneId);
} else {
return ((LocalDate) getTemporal()).atStartOfDay().atZone(zoneId);
}
} else if (isUtc(getTemporal())) {
return ((Instant) getTemporal()).atZone(zoneId);
} else {
Expand Down

0 comments on commit 1d83736

Please sign in to comment.