JSP定制标记——分析java.util.Locale,根据地区选择显示日期的标记


package org.eleaf.java.guestbook.tags;

import java.util.*;
import java.text.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;


public class NowDateTag extends TagSupport{
private String prefix = "", language = "", country = "";
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setCountry(String country) {
this.country = country;
}
public void setLanguage(String language) {
this.language = language;
}
private String filterLanguage(String language) { //分析请求头Accept-Language
if (language.length() >=2) {
language = language.substring(0,2);
}
return language;
}
public int doStartTag() throws JspException {
try {
Date date = new Date();
Locale locale = new Locale(filterLanguage(language), country);
String nowDate = prefix + " " + getNowDate(date, locale) + " " + getDayOfWeek(date, locale);
pageContext.getOut().println(nowDate);
} catch (Exception e) {
throw new JspException(e);
}
return SKIP_BODY;
}
private String getNowDate(Date date, Locale locale) {
String nowDate = "";
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, locale);
nowDate += df.format(date);
return nowDate;
}
private String getDayOfWeek(Date date, Locale locale) {
Calendar cal = Calendar.getInstance(locale);
cal.setTime(date);
int index = cal.get(Calendar.DAY_OF_WEEK);
DateFormatSymbols symbols = new DateFormatSymbols(locale);
String[] days = symbols.getWeekdays();
return days[index];
}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC '-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.2//EN' 'http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd'>

<taglib>
<tlib-version>1.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Html Tags</short-name>
<tag>
<name>nowDate</name>
<tag-class>org.eleaf.java.guestbook.tags.NowDateTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>prefix</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>language</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>country</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.util.*" %>
<%@ taglib prefix="htm" uri="/WEB-INF/tlds/html-tag.tld" %>
<htm:nowDate prefix="今天是" language="zh" country="cn" />
<br>
<htm:nowDate prefix="美国版本:" language="en" country="" />
<br>
<htm:nowDate prefix="英国版本:" language="en" country="uk" />
<br>
<htm:nowDate prefix="日本版本:" language="ja" country="jp" />
<br>
<htm:nowDate prefix="台湾地区版本:" language="zh" country="tw" />
<br>
<htm:nowDate prefix="法国版本:" language="fr" country="cn" />
<br>
<htm:nowDate prefix="根据浏览器确定:" language="<%=request.getHeader("Accept-language")%>" country="" />
<br>

<%
Locale loc = new Locale("fr", "cn");
out.println(loc.getDisplayLanguage() + ":" + loc.getDisplayCountry() + "<br>");
loc = new Locale("fr", "fr");
out.println(loc.getDisplayLanguage() + ":" + loc.getDisplayCountry() + "<br>");
loc = new Locale("fr", "错误国家代码");
out.println(loc.getDisplayLanguage() + ":" + loc.getDisplayCountry() + "<br>");
%>

结果:

今天是 2005年2月17日 星期四
美国版本: February 17, 2005 Thursday
英国版本: February 17, 2005 Thursday
日本版本: 2005/02/17 木曜日
台湾地区版本: 2005年2月17日 星期四
法国版本: 17 février 2005 jeudi
根据浏览器确定: 2005年2月17日 星期四
法文:中国
法文:法国
法文:错误国家代码

你可能感兴趣的:(java,jsp,Web,servlet,浏览器)