Example usage for java.time OffsetDateTime plusDays

List of usage examples for java.time OffsetDateTime plusDays

Introduction

In this page you can find the example usage for java.time OffsetDateTime plusDays.

Prototype

public OffsetDateTime plusDays(long days) 

Source Link

Document

Returns a copy of this OffsetDateTime with the specified number of days added.

Usage

From source file:org.silverpeas.core.calendar.CalendarEventOccurrence.java

/**
 * Gets optionally an event occurrence by its identifier.
 * <p>If the occurrence exists into the persistence, it is returned. Otherwise it is generated.
 * <p>Otherwise and if start date is valid, the occurrence is generated.
 * @param id the identifier of the aimed occurrence.
 * @return an optional calendar event occurrence.
 */// w  w w .  ja  va 2 s  . c  om
public static Optional<CalendarEventOccurrence> getById(final String id) {
    final CalendarEventOccurrenceRepository repository = CalendarEventOccurrenceRepository.get();
    final Mutable<CalendarEventOccurrence> occurrence = Mutable.ofNullable(repository.getById(id));
    if (!occurrence.isPresent()) {
        final Pair<String, Temporal> explodedId = explodeId(id);
        final String eventId = explodedId.getLeft();
        final Temporal startDate = explodedId.getRight();
        final Optional<CalendarEvent> event = Optional.ofNullable(CalendarEvent.getById(eventId));
        event.ifPresent(e -> {
            if (e.isRecurrent()) {
                final LocalDate occStartDate;
                final LocalDate occEndDate;
                if (startDate instanceof LocalDate) {
                    final LocalDate date = (LocalDate) startDate;
                    occStartDate = date.minusDays(1);
                    occEndDate = date.plusDays(1);
                } else {
                    final OffsetDateTime dateTime = (OffsetDateTime) startDate;
                    occStartDate = dateTime.minusDays(1).toLocalDate();
                    occEndDate = dateTime.plusDays(1).toLocalDate();
                }
                final List<CalendarEventOccurrence> occurrences = e.getCalendar()
                        .between(occStartDate, occEndDate).getEventOccurrences();
                occurrences.removeIf(o -> !o.getCalendarEvent().getId().equals(eventId)
                        || (!o.getStartDate().equals(startDate)));
                if (occurrences.size() == 1) {
                    occurrence.set(occurrences.get(0));
                }
            } else {
                occurrence.set(new CalendarEventOccurrence(e, e.getStartDate(), e.getEndDate()));
            }
        });
    }
    return Optional.ofNullable(occurrence.orElse(null));
}