Example usage for java.time YearMonth plusMonths

List of usage examples for java.time YearMonth plusMonths

Introduction

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

Prototype

public YearMonth plusMonths(long monthsToAdd) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    YearMonth s = y.plusMonths(3);
    System.out.println(s);//from  ww w.  j  ava2 s  .co m

}

From source file:com.github.drbookings.LocalDates.java

public static boolean isNextMonth(YearMonth selectedMonth, LocalDate date) {
    return YearMonth.from(date).equals(selectedMonth.plusMonths(1));
}

From source file:sg.ncl.MainController.java

@PostMapping("/admin/statistics")
public String adminUsageStatisticsQuery(@Valid @ModelAttribute("query") ProjectUsageQuery query,
        BindingResult result, RedirectAttributes attributes, HttpSession session) {
    if (!validateIfAdmin(session)) {
        return NO_PERMISSION_PAGE;
    }/*from w w  w  .  j ava 2 s .c o  m*/

    List<ProjectDetails> newProjects = new ArrayList<>();
    List<ProjectDetails> activeProjects = new ArrayList<>();
    List<ProjectDetails> inactiveProjects = new ArrayList<>();
    List<ProjectDetails> stoppedProjects = new ArrayList<>();
    List<String> months = new ArrayList<>();
    Map<String, MonthlyUtilization> utilizationMap = new HashMap<>();
    Map<String, Integer> statsCategoryMap = new HashMap<>();
    Map<String, Integer> statsAcademicMap = new HashMap<>();
    int totalCategoryUsage = 0;
    int totalAcademicUsage = 0;

    if (result.hasErrors()) {
        StringBuilder message = new StringBuilder();
        message.append(TAG_ERRORS);
        message.append(TAG_UL);
        for (ObjectError objectError : result.getAllErrors()) {
            FieldError fieldError = (FieldError) objectError;
            message.append(TAG_LI);
            message.append(fieldError.getField());
            message.append(TAG_SPACE);
            message.append(fieldError.getDefaultMessage());
            message.append(TAG_LI_CLOSE);
        }
        message.append(TAG_UL_CLOSE);
        attributes.addFlashAttribute(MESSAGE, message.toString());
    } else {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("MMM-yyyy").toFormatter();
        YearMonth m_s = YearMonth.parse(query.getStart(), formatter);
        YearMonth m_e = YearMonth.parse(query.getEnd(), formatter);

        YearMonth counter = m_s;
        while (!counter.isAfter(m_e)) {
            String monthYear = counter.format(formatter);
            utilizationMap.put(monthYear, new MonthlyUtilization(monthYear));
            months.add(monthYear);
            counter = counter.plusMonths(1);
        }
        List<ProjectDetails> projectsList = getProjects();

        for (ProjectDetails project : projectsList) {
            // compute active and inactive projects
            differentiateProjects(newProjects, activeProjects, inactiveProjects, stoppedProjects, m_s, m_e,
                    project);

            // monthly utilisation
            computeMonthlyUtilisation(utilizationMap, formatter, m_s, m_e, project);

            // usage statistics by category
            totalCategoryUsage += getCategoryUsage(statsCategoryMap, m_s, m_e, project);

            // usage statistics by academic institutes
            totalAcademicUsage += getAcademicUsage(statsAcademicMap, m_s, m_e, project);
        }
    }

    attributes.addFlashAttribute(KEY_QUERY, query);
    attributes.addFlashAttribute("newProjects", newProjects);
    attributes.addFlashAttribute("activeProjects", activeProjects);
    attributes.addFlashAttribute("inactiveProjects", inactiveProjects);
    attributes.addFlashAttribute("stoppedProjects", stoppedProjects);
    attributes.addFlashAttribute("months", months);
    attributes.addFlashAttribute("utilization", utilizationMap);
    attributes.addFlashAttribute("statsCategory", statsCategoryMap);
    attributes.addFlashAttribute("totalCategoryUsage", totalCategoryUsage);
    attributes.addFlashAttribute("statsAcademic", statsAcademicMap);
    attributes.addFlashAttribute("totalAcademicUsage", totalAcademicUsage);

    return "redirect:/admin/statistics";
}

From source file:sg.ncl.MainController.java

private void computeMonthlyUtilisation(Map<String, MonthlyUtilization> utilizationMap,
        DateTimeFormatter formatter, YearMonth m_s, YearMonth m_e, ProjectDetails project) {
    YearMonth counter = m_s;
    while (!counter.isAfter(m_e)) {
        String monthYear = counter.format(formatter);
        int usageSum = project.getProjectUsages().stream().filter(p -> p.getMonth().equals(monthYear))
                .mapToInt(ProjectUsage::getUsage).sum();
        utilizationMap.get(monthYear).addNodeHours(usageSum);
        counter = counter.plusMonths(1);
    }//from  w w  w.  jav  a 2 s.c  o m
}