Example usage for java.time LocalDateTime plusMonths

List of usage examples for java.time LocalDateTime plusMonths

Introduction

In this page you can find the example usage for java.time LocalDateTime plusMonths.

Prototype

public LocalDateTime plusMonths(long months) 

Source Link

Document

Returns a copy of this LocalDateTime with the specified number of months added.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 00);

    LocalDateTime t = a.plusMonths(100);

    System.out.println(t);//from www.  j av a  2s . c  o m
}

From source file:de.steilerdev.myVerein.server.controller.admin.EventManagementController.java

/**
 * This function gathers all dates where an event takes place within a specific month and year. The function is invoked by GETting the URI /api/admin/event/month and specifying the month and year via the request parameters.
 * @param month The selected month.//w  w  w  .  ja  v a2  s .  c o  m
 * @param year The selected year.
 * @return An HTTP response with a status code. If the function succeeds, a list of dates, during the month, that contain an event, is returned, otherwise an error code is returned.
 */
@RequestMapping(value = "month", produces = "application/json", method = RequestMethod.GET)
public ResponseEntity<List<LocalDate>> getEventDatesOfMonth(@RequestParam String month,
        @RequestParam String year, @CurrentUser User currentUser) {
    logger.trace("[" + currentUser + "] Gathering events of month " + month + " and year " + year);
    ArrayList<LocalDate> dates = new ArrayList<>();
    int monthInt, yearInt;
    try {
        monthInt = Integer.parseInt(month);
        yearInt = Integer.parseInt(year);
    } catch (NumberFormatException e) {
        logger.warn("[" + currentUser + "] Unable to parse month or year.");
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    LocalDateTime start = LocalDate.of(yearInt, monthInt, 1).atStartOfDay();
    LocalDateTime end = start.plusMonths(1);

    logger.debug("Getting all single day events...");
    dates.addAll(eventRepository.findAllByEndDateTimeBetweenAndMultiDate(start, end, false).parallelStream()
            .map(Event::getStartDate).collect(Collectors.toList()));
    logger.debug("All single day events retrieved, got " + dates.size() + " dates so far");

    logger.debug("Getting all multi day events...");
    //Collecting all multi date events, that either start or end within the selected month (which means that events that are spanning over several months are not collected)
    Stream.concat(eventRepository.findAllByStartDateTimeBetweenAndMultiDate(start, end, true).stream(), //All multi date events starting within the month
            eventRepository.findAllByEndDateTimeBetweenAndMultiDate(start, end, true).stream()) //All multi date events ending within the month
            .distinct() //Removing all duplicated events
            .parallel().forEach(event -> { //Creating a local date for each occupied date of the event
                for (LocalDate date = event.getStartDate(); //Starting with the start date
                        !date.equals(event.getEndDateTime().toLocalDate()); //Until reaching the end date
                        date = date.plusDays(1)) //Adding a day within each iterations
                {
                    dates.add(date);
                }
                dates.add(event.getEndDateTime().toLocalDate()); //Finally adding the last date
            });
    logger.debug("All multi day events gathered, got " + dates.size() + " dates so far");

    if (dates.isEmpty()) {
        logger.warn("[" + currentUser + "] Returning empty dates list of " + month + "/" + year);
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    } else {
        logger.debug("[" + currentUser + "] Returning dates list " + month + "/" + year);
        return new ResponseEntity<>(dates.stream().distinct().collect(Collectors.toList()), HttpStatus.OK); //Returning an optimized set of events
    }
}