Example usage for java.time Instant now

List of usage examples for java.time Instant now

Introduction

In this page you can find the example usage for java.time Instant now.

Prototype

public static Instant now(Clock clock) 

Source Link

Document

Obtains the current instant from the specified clock.

Usage

From source file:com.gigglinggnus.controllers.CheckinController.java

private JSONArray getAppointments(User user, Term term, Clock clk) {
    Instant now = Instant.now(clk);
    //return a list of appointments in this term
    List<Appointment> appts = user.getAppointments().stream().filter(appt -> appt.getTerm().equals(term))
            .filter(appt -> !appt.isStudentCheckedIn()).collect(Collectors.toList());
    //sorted by distance to current time
    Collections.sort(appts,/*from w ww . jav a 2 s  .  c  o m*/
            (Appointment a1, Appointment a2) -> Duration.between(now, a1.getInterval().getStart()).abs()
                    .compareTo(Duration.between(now, a1.getInterval().getStart()).abs()));

    JSONArray json = new JSONArray();

    for (Appointment appt : appts) {
        String examId = appt.getExam().getExamId();
        String startTime = LocalDateTime.from(appt.getInterval().getStart().atZone(ZoneId.systemDefault()))
                .toString();
        String seatNum = appt.prettySeat();
        //write JSON
        JSONObject elem = new JSONObject();
        elem.put("exam", examId);
        elem.put("start", startTime);
        elem.put("seat", seatNum);
        json.put(elem);
    }
    return json;
}