Servlet 3.1 国际化 - 区域设置 - 日期
Servlet 可以使用 java.text.DateFormat
类及其静态方法 getDateTimeInstance()
来格式化特定于区域设置的日期和时间
cn/twle/demo/DateLocaleServlet.java
// author: 简单教程(www.twle.cn) // Copyright © 2015-2065 www.twle.cn. All rights reserved. package cn.twle.demo; import java.io.*; import java.util.Locale; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.DateFormat; import java.util.Date; @WebServlet(name = "DateLocaleServlet", urlPatterns = {"date_locale"}, loadOnStartup = 1) public class DateLocaleServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 获取客户端的区域设置 Locale locale = request.getLocale( ); String date = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.SHORT, locale).format(new Date( )); String title = "特定于区域设置的日期 - Servlet 基础教程 | 简单教程(www.twle.cn)"; String docType = "<!DOCTYPE html> \n"; out.println(docType + "<title>" + title + "</title>" + "<p>" + title + "</p>" + "<p>" + date + "</p>"); } }
在浏览器上输入 http://localhost:8080/servlet/date_locale 显示结果如下