JSP 可直接使用 Java 標準庫 java.util.Date、Calendar 及格式化類別 SimpleDateFormat (舊版) 或 DateTimeFormatter (Java 8+)。本文涵蓋建立、比較、格式化、解析與時區處理。
// 當前時間 Date now = new Date(); // 指定毫秒數 (自 1970-01-01 00:00:00 UTC) Date specific = new Date(1699999999000L); // 常用方法 now.getTime() // 取得毫秒數 (long) now.before(another) // 是否早於另一日期 now.after(another) // 是否晚於另一日期 now.compareTo(another) // 比較: -1/0/1 now.toString() // 預設格式字串
<%@ page import="java.util.Date" %> <p>現在時間:<%= new Date() %></p> <!-- 輸出範例: Mon Nov 13 14:30:45 CST 2023 -->
| 方式 | 範例 | 適用場景 |
|---|---|---|
| getTime() 比較毫秒 | d1.getTime() < d2.getTime() | 精確比較、排序 |
| before/after/equals | d1.before(d2) | 語意清晰、可讀性佳 |
| compareTo() | d1.compareTo(d2) < 0 | 實作 Comparable 介面、集合排序 |
| 字元 | 意義 | 範例 |
|---|---|---|
| y | 年 | 2023 / 23 |
| M | 月 | 11 / Nov / November |
| d | 日 | 13 |
| H | 時 (0-23) | 14 |
| h | 時 (1-12) | 02 |
| m | 分 | 30 |
| s | 秒 | 45 |
| S | 毫秒 | 123 |
| E | 星期 | Mon / 星期一 |
| a | AM/PM | PM |
| z | 時區 | CST / GMT+08:00 |
<%@ page import="java.text.SimpleDateFormat, java.util.Date, java.util.Locale" %>
<%
Date now = new Date();
// 格式化
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatted = sdf1.format(now); // 2023-11-13 14:30:45
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 E a hh:mm", Locale.TRADITIONAL_CHINESE);
String chinese = sdf2.format(now); // 2023年11月13日 星期一 下午 02:30
// 解析
SimpleDateFormat parser = new SimpleDateFormat("yyyy/MM/dd");
Date parsed = parser.parse("2023/12/25"); // 不拋出 ParseException 需 try-catch
%>
Thread-safe、不可變、API 直覺:
<%@ page import="java.time.*, java.time.format.DateTimeFormatter" %>
<%
// 當前日期時間 (系統預設時區)
LocalDateTime now = LocalDateTime.now();
LocalDate today = LocalDate.now();
LocalTime time = LocalTime.now();
ZonedDateTime zdt = ZonedDateTime.now(); // 含時區
// 格式化
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String str = now.format(fmt);
// 解析
LocalDate parsed = LocalDate.parse("2023-12-25", DateTimeFormatter.ISO_LOCAL_DATE);
// 運算
LocalDate nextWeek = today.plusWeeks(1);
Period age = Period.between(birthday, today);
// 時區轉換
ZonedDateTime taipei = ZonedDateTime.now(ZoneId.of("Asia/Taipei"));
ZonedDateTime utc = taipei.withZoneSameInstant(ZoneOffset.UTC);
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.time.*, java.time.format.DateTimeFormatter, java.util.Locale" %>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>日期時間展示</title></head>
<body>
<h2>Java 8+ 日期時間 API 展示</h2>
<ul>
<li>ISO 格式:<%= LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) %></li>
<li>中文完整:<%= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 EEEE a hh:mm:ss", Locale.TRADITIONAL_CHINESE)) %></li>
<li>Unix 時間戳:<%= Instant.now().getEpochSecond() %></li>
<li>台北時區:<%= ZonedDateTime.now(ZoneId.of("Asia/Taipei")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")) %></li>
</ul>
</body></html>
SimpleDateFormat 非執行緒安全問題。static final 或 ThreadLocal,勿在請求中重複建立。Locale 與資源包,支援多語系日期顯示。