Example usage for java.time YearMonth format

List of usage examples for java.time YearMonth format

Introduction

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

Prototype

public String format(DateTimeFormatter formatter) 

Source Link

Document

Formats this year-month using the specified formatter.

Usage

From source file:Main.java

public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    String s = y.format(DateTimeFormatter.ofPattern("yyyy MM"));
    System.out.println(s);/*ww  w  .j  a v  a  2  s  .  co m*/

}

From source file:Main.java

public static String toUUUUMM(YearMonth yearAndMonth) {
    return yearAndMonth.format(DateTimeFormatter.ofPattern("uuuuMM"));
}

From source file:fr.lepellerin.ecole.web.controller.cantine.DetaillerReservationRepasController.java

/**
 * initialise le form.//  w ww.  j a va  2s . c  o m
 *
 * @return <code>DetaillerReservationRepasForm</code>
 */
@ModelAttribute("command")
public DetaillerReservationRepasForm addCommand() {
    final DetaillerReservationRepasForm form = new DetaillerReservationRepasForm();
    final YearMonth moisActuel = YearMonth.now();
    final Integer intMois = Integer
            .valueOf(moisActuel.format(DateTimeFormatter.ofPattern(GeDateUtils.DATE_FORMAT_YYYYMM)));
    form.setAnneeMois(intMois);
    return form;
}

From source file:org.opensingular.form.type.util.STypeYearMonth.java

@Override
protected String toStringPersistence(YearMonth originalValue) {
    return originalValue == null ? null : originalValue.format(formatter());
}

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;
    }/*  w w w.j  av a2 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);
    }//w  w w .  j  a va2 s .  co  m
}