SimpleDateFormat là lớp cụ thể để định dạng và phân tích ngày tháng theo locale. Cho phép chọn pattern do người dùng định nghĩa cho date-time formatting.
Ví dụ
<%@ page import = "java.io.*,java.util.*" %>
<%@ page import = "javax.servlet.*,java.text.*" %>
<html>
<head> <title>Display Current Date & Time</title> </head>
<body>
<center> <h1>Display Current Date & Time</h1> </center>
<%
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
out.print("<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>
</body>
</html>
Biên dịch và gọi qua https://localhost:8080/CurrentDate. Kết quả:
Display Current Date & Time Mon 2010.06.21 at 10:06:44 PM GMT+04:00