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:Main.java

public static void main(String[] args) {
    Instant instant = Instant.now(Clock.systemDefaultZone());
    System.out.println(instant.getEpochSecond());

}

From source file:Main.java

public static void main(String[] args) {
    Clock defaultClock = Clock.systemDefaultZone();

    Instant instant = Instant.now(defaultClock);

    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());

    System.out.println(zonedDateTime);
}

From source file:Main.java

public static void main(String[] args) {
    Clock defaultClock = Clock.systemDefaultZone();

    Instant instant = Instant.now(defaultClock);

    TemporalQuery query = (TemporalAccessor x) -> x.isSupported(ChronoField.MILLI_OF_DAY);

    System.out.println(instant.query(query));
}

From source file:Main.java

public static void main(String[] args) {
    Clock clock = Clock.systemDefaultZone();

    Instant instant1 = clock.instant();
    System.out.println(instant1);

    Instant instant2 = Instant.now(clock);
    System.out.println(instant2);

    LocalDate localDate = LocalDate.now(clock);
    System.out.println(localDate);

    ZonedDateTime zoneDateTime = ZonedDateTime.now(clock);
    System.out.println(zoneDateTime);
}

From source file:Main.java

public static void main(String[] args) {
    Clock utcClock = Clock.systemUTC();
    Clock defaultClock = Clock.systemDefaultZone();
    Clock offsetClock = Clock.offset(Clock.systemUTC(), Duration.ofHours(-5));

    ZoneId denverTimeZone = ZoneId.of("America/Denver");
    ZoneId newYorkTimeZone = ZoneId.of("America/New_York");
    ZoneId chicagoTimeZone = ZoneId.of("America/Chicago");
    ZoneId losAngelesTimeZone = ZoneId.of("America/Los_Angeles");

    Instant instant = Instant.now(defaultClock);
    Instant instant2 = Instant.now(utcClock);
    Instant instant3 = Instant.now(offsetClock);

    System.out.println(instant);// ww  w.  j a v a2  s. c om
    System.out.println(instant2);
    System.out.println(instant3.plus(Duration.ofSeconds(90)));
    System.out.println(instant3.atZone(newYorkTimeZone));
    System.out.println(instant3.atZone(chicagoTimeZone));
    System.out.println(instant3.atZone(denverTimeZone));
    System.out.println(instant3.atZone(losAngelesTimeZone));
}

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");

    Clock clk = (Clock) (request.getSession().getAttribute("clock"));

    List<Term> terms = Term.getFutureTerms(em, Instant.now(clk));
    List<Exam> exams = terms.stream().flatMap(term -> term.getExams().stream())
            .filter(exam -> exam.getStatus() == ExamStatus.PENDING).collect(Collectors.toList());

    Map<Exam, ArrayList<String>> utilMap = new HashMap();
    for (Exam e : exams) {
        Interval<Instant> examInt = e.getInterval();

        LocalDate testDate = examInt.getStart().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = examInt.getEnd().atZone(ZoneId.systemDefault()).toLocalDate();

        Term t = e.getTerm();//w w  w  . j av  a  2  s .c  o m
        Map<LocalDate, Long> examUtilMap = new HashMap();
        ArrayList<String> examList = new ArrayList();
        while (testDate.isBefore(endDate.plusDays(1))) {

            examList.add(testDate.toString() + "=" + t.utilizationForDay(testDate, clk) + "   ");

            testDate = testDate.plusDays(1);
        }
        utilMap.put(e, examList);

    }

    request.setAttribute("exams", exams);
    request.setAttribute("utilList", utilMap);
    RequestDispatcher rd = request.getRequestDispatcher("/admin/manage-exams.jsp");
    rd.forward(request, response);
}

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");

    Clock clk = (Clock) (request.getSession().getAttribute("clock"));

    List<Term> terms = Term.getFutureTerms(em, Instant.now(clk));
    List<Exam> exams = terms.stream().flatMap(term -> term.getExams().stream())
            .filter(exam -> exam.getStatus() == ExamStatus.PENDING).collect(Collectors.toList());

    request.setAttribute("exams", exams);

    RequestDispatcher rd = request.getRequestDispatcher("/student/make-apmt.jsp");
    rd.forward(request, response);//from  www  . j  av a2  s.  c om
}

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");

    Clock clk = (Clock) (request.getSession().getAttribute("clock"));

    List<Term> terms = Term.getFutureTerms(em, Instant.now(clk));
    List<Exam> exams = terms.stream().flatMap(term -> term.getExams().stream())
            .filter(exam -> exam.getStatus() == ExamStatus.PENDING).collect(Collectors.toList());

    request.setAttribute("exams", exams);

    RequestDispatcher rd = request.getRequestDispatcher("/admin/make-apmt.jsp");
    rd.forward(request, response);//from  w  w  w .j a  v  a  2 s  .  c o m
}

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

/**
 *
 * @param request servlet request/*  w w w  . j  ava 2  s .  c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String msg = request.getParameter("msg");
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");
    Clock clk = (Clock) (request.getSession().getAttribute("clock"));
    Term term = Term.getTermforInstant(em, Instant.now(clk));
    LocalDate start = LocalDate.parse(request.getParameter("start"));
    LocalDate end = LocalDate.parse(request.getParameter("end"));

    JSONArray json = new JSONArray();
    for (LocalDate i = start; i.isBefore(end); i = i.plusDays(1)) {
        double utilization = term.utilizationForDay(start, clk);
        JSONObject elem = new JSONObject();
        elem.put("day", i);
        elem.put("utilization", utilization);
        json.put(elem);
    }
    response.getWriter().write(json.toString());
}

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

/**
 *
 * @param request servlet request//from  w w  w.  j a v  a  2s.  c  o m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String msg = request.getParameter("msg");
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");
    Clock clk = (Clock) (request.getSession().getAttribute("clock"));
    Term term = Term.getTermforInstant(em, Instant.now(clk));
    if (msg.equals("get_appointments")) {
        String userId = request.getParameter("username");
        User user = em.find(User.class, userId);

        JSONArray json = getAppointments(user, term, clk);
        response.getWriter().write(json.toString());
    } else if (msg.equals("do_checkin")) {
        String userId = request.getParameter("username");
        String examId = request.getParameter("examid");
        User admin = (User) (request.getSession().getAttribute("user"));
        User student = em.find(User.class, userId);
        Exam exam = em.find(Exam.class, examId);

        Appointment appt = student.getAppointmentByExam(exam);
        em.getTransaction().begin();
        admin.checkInStudent(appt);
        em.persist(appt);
        em.persist(student);
        em.getTransaction().commit();

        JSONArray json = getAppointments(student, term, clk);
        response.getWriter().write(json.toString());
    } else {
        JSONObject json = new JSONObject();
        json.put("error", msg);
        response.getWriter().write(json.toString());
    }
}