Example usage for java.time ZonedDateTime format

List of usage examples for java.time ZonedDateTime format

Introduction

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

Prototype

@Override 
public String format(DateTimeFormatter formatter) 

Source Link

Document

Formats this date-time using the specified formatter.

Usage

From source file:sg.ncl.MainController.java

@RequestMapping("/admin/usage")
public String adminTeamUsage(Model model, @RequestParam(value = "start", required = false) String start,
        @RequestParam(value = "end", required = false) String end,
        @RequestParam(value = "organizationType", required = false) String organizationType,
        @RequestParam(value = "team", required = false) String team,
        final RedirectAttributes redirectAttributes, HttpSession session)
        throws IOException, WebServiceRuntimeException {
    if (!validateIfAdmin(session)) {
        return NO_PERMISSION_PAGE;
    }//from   w  ww  . j  a  va 2s. c  o m

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    ZonedDateTime nowDate = ZonedDateTime.now();
    String now = nowDate.format(formatter);
    if (start == null) {
        start = nowDate.with(firstDayOfMonth()).format(formatter);
    }
    if (end == null) {
        end = now;
    }
    if (now.compareTo(start) < 0 || now.compareTo(end) < 0) {
        redirectAttributes.addFlashAttribute(MESSAGE, "Period selected is beyond current date (today).");
        return REDIRECT_TEAM_USAGE;
    }

    // get list of teamids
    HttpEntity<String> request = createHttpEntityHeaderOnly();
    ResponseEntity responseEntity = restTemplate.exchange(properties.getSioTeamsUrl(), HttpMethod.GET, request,
            String.class);
    JSONArray jsonArray = new JSONArray(responseEntity.getBody().toString());

    List<Team2> searchTeams = new ArrayList<>();
    TeamManager2 teamManager2 = new TeamManager2();
    getSearchTeams(organizationType, team, jsonArray, searchTeams, teamManager2);

    if (!searchTeams.isEmpty()) {
        List<String> dates = getDates(start, end, formatter);

        Map<String, List<Long>> teamUsages = new HashMap<>();
        Long totalUsage = 0L;
        for (Team2 team2 : searchTeams) {
            try {
                List<Long> usages = new ArrayList<>();
                totalUsage += getTeamUsageStatistics(team2, start, end, request, usages);
                teamUsages.put(team2.getName(), usages);
            } catch (RestClientException rce) {
                log.warn("Error connecting to sio analytics service for team usage: {}", rce);
                redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD);
                return REDIRECT_TEAM_USAGE;
            } catch (StartDateAfterEndDateException sde) {
                redirectAttributes.addFlashAttribute(MESSAGE, ERR_START_DATE_AFTER_END_DATE);
                return REDIRECT_TEAM_USAGE;
            }
        }
        model.addAttribute("dates", dates);
        model.addAttribute("teamUsages", teamUsages);
        model.addAttribute("totalUsage", totalUsage);
    }

    List<Team2> allTeams = new ArrayList<>(teamManager2.getTeamMap().values());
    allTeams.sort(Comparator.comparing(Team2::getName, String.CASE_INSENSITIVE_ORDER));
    model.addAttribute(ALL_TEAMS, allTeams);
    model.addAttribute("start", start);
    model.addAttribute("end", end);
    model.addAttribute("organizationType", organizationType);
    model.addAttribute("team", team);
    return "usage_statistics";
}

From source file:sg.ncl.MainController.java

@RequestMapping(value = "/admin/energy", method = RequestMethod.GET)
public String adminEnergy(Model model, @RequestParam(value = "start", required = false) String start,
        @RequestParam(value = "end", required = false) String end, final RedirectAttributes redirectAttributes,
        HttpSession session) throws IOException {

    if (!validateIfAdmin(session)) {
        return NO_PERMISSION_PAGE;
    }//from  w  w w.j av  a2 s.  c  om

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    ZonedDateTime now = ZonedDateTime.now();
    if (start == null) {
        ZonedDateTime startDate = now.with(firstDayOfMonth());
        start = startDate.format(formatter);
    }
    if (end == null) {
        ZonedDateTime endDate = now.with(lastDayOfMonth());
        end = endDate.format(formatter);
    }

    HttpEntity<String> request = createHttpEntityHeaderOnly();

    ResponseEntity responseEntity;
    try {
        responseEntity = restTemplate.exchange(
                properties.getEnergyStatistics("startDate=" + start, "endDate=" + end), HttpMethod.GET, request,
                String.class);
    } catch (RestClientException e) {
        log.warn("Error connecting to sio analytics service for energy usage: {}", e);
        redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD);
        return REDIRECT_ENERGY_USAGE;
    }

    String responseBody = responseEntity.getBody().toString();
    JSONArray jsonArray = new JSONArray(responseBody);

    // handling exceptions from SIO
    if (RestUtil.isError(responseEntity.getStatusCode())) {
        MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class);
        ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError());
        switch (exceptionState) {
        case START_DATE_AFTER_END_DATE_EXCEPTION:
            log.warn("Get energy usage : Start date after end date error");
            redirectAttributes.addFlashAttribute(MESSAGE, ERR_START_DATE_AFTER_END_DATE);
            return REDIRECT_ENERGY_USAGE;

        default:
            log.warn("Get energy usage : sio or deterlab adapter connection error");
            redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD);
            return REDIRECT_ENERGY_USAGE;
        }
    } else {
        log.info("Get energy usage info : {}", responseBody);
    }

    DecimalFormat df2 = new DecimalFormat(".##");

    double sumEnergy = 0.00;
    List<String> listOfDate = new ArrayList<>();
    List<Double> listOfEnergy = new ArrayList<>();
    ZonedDateTime currentZonedDateTime = convertToZonedDateTime(start);
    String currentDate = null;
    for (int i = 0; i < jsonArray.length(); i++) {
        sumEnergy += jsonArray.getDouble(i);

        // add into listOfDate to display graph
        currentDate = currentZonedDateTime.format(formatter);
        listOfDate.add(currentDate);

        // add into listOfEnergy to display graph
        double energy = Double.valueOf(df2.format(jsonArray.getDouble(i)));
        listOfEnergy.add(energy);

        currentZonedDateTime = convertToZonedDateTime(currentDate).plusDays(1);
    }

    sumEnergy = Double.valueOf(df2.format(sumEnergy));
    model.addAttribute("listOfDate", listOfDate);
    model.addAttribute("listOfEnergy", listOfEnergy);
    model.addAttribute("start", start);
    model.addAttribute("end", end);
    model.addAttribute("energy", sumEnergy);
    return "energy_usage";
}

From source file:sg.ncl.MainController.java

/**
 * @param zonedDateTimeJSON JSON string//from w w w  .j av a 2 s  .c om
 * @return a date in the format MMM-d-yyyy
 */
protected String formatZonedDateTime(String zonedDateTimeJSON) throws Exception {
    ZonedDateTime zonedDateTime = getZonedDateTime(zonedDateTimeJSON);
    DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM-d-yyyy");
    return zonedDateTime.format(format);
}

From source file:sg.ncl.MainController.java

private List<String> getDates(String start, String end, DateTimeFormatter formatter) {
    List<String> dates = new ArrayList<>();
    ZonedDateTime currentZonedDateTime = convertToZonedDateTime(start);
    ZonedDateTime endZoneDateTime = convertToZonedDateTime(end);
    while (currentZonedDateTime.isBefore(endZoneDateTime)) {
        String date = currentZonedDateTime.format(formatter);
        dates.add(date);/* w w w.  jav  a  2  s  .  c o  m*/
        currentZonedDateTime = currentZonedDateTime.plusDays(1);
    }
    dates.add(currentZonedDateTime.format(formatter));
    return dates;
}