Example usage for java.time YearMonth isAfter

List of usage examples for java.time YearMonth isAfter

Introduction

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

Prototype

public boolean isAfter(YearMonth other) 

Source Link

Document

Checks if this year-month is after the specified year-month.

Usage

From source file:Main.java

public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    System.out.println(y.isAfter(YearMonth.now()));

}

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 ww  w.ja  v  a 2  s .  c  om*/

    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 differentiateProjects(List<ProjectDetails> newProjects, List<ProjectDetails> activeProjects,
        List<ProjectDetails> inactiveProjects, List<ProjectDetails> stoppedProjects, YearMonth m_s,
        YearMonth m_e, ProjectDetails project) {
    YearMonth created = YearMonth.from(project.getZonedDateCreated());
    YearMonth m_e_m1 = m_e.minusMonths(1);
    YearMonth m_e_m2 = m_e.minusMonths(2);
    YearMonth m_active = m_e_m2.isBefore(m_s) ? m_e_m2 : m_s;

    // projects created within the period
    if (!(created.isBefore(m_s) || created.isAfter(m_e))) {
        newProjects.add(project);/*from  w ww  .j a  va  2  s  .  c  om*/
    }

    // active projects = projects with resources within the period + projects created
    boolean hasUsage = project.getProjectUsages().stream().anyMatch(p -> p.hasUsageWithinPeriod(m_active, m_e));
    if (hasUsage || !(created.isBefore(m_e_m2) || created.isAfter(m_e))) {
        activeProjects.add(project);
    }

    // inactive projects
    if (!hasUsage && created.isBefore(m_e_m2)) {
        inactiveProjects.add(project);
    }

    // stopped projects
    boolean hasUsagePreviousMonth = project.getProjectUsages().stream()
            .anyMatch(p -> p.hasUsageWithinPeriod(m_e_m1, m_e_m1));
    boolean hasUsageCurrentMonth = project.getProjectUsages().stream()
            .anyMatch(p -> p.hasUsageWithinPeriod(m_e, m_e));
    if (hasUsagePreviousMonth && !hasUsageCurrentMonth) {
        stoppedProjects.add(project);
    }
}

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);
    }/* w w w .  j  ava  2  s.com*/
}