Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình

In ngày giờ hiện tại trong trang JSP

Với JSP, rất dễ lấy ngày giờ hiện tại. Dùng đối tượng Date đơn giản với phương thức toString() in ra ngày giờ:

Ví dụ

<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>
<html>
  <head> <title>Display Current Date & Time</title> </head>
  <body>
    <center> <h1>Display Current Date & Time</h1> </center>
    <%
      Date date = new Date();
      out.print("<h2 align=\"center\">" + date.toString() + "</h2>");
    %>
  </body>
</html>

Lưu code vào CurrentDate.jsp, gọi qua https://localhost:8080/CurrentDate.jsp. Kết quả:

Display Current Date & Time
Mon Jun 21 21:46:49 GMT+04:00 2010

Làm mới trang https://localhost:8080/CurrentDate.jsp. Mỗi lần refresh, giây sẽ thay đổi.