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) {// analyzes the request header 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.get (). Println (nowDate);} catch (exception e) {throw new jspexception (e);} return skip_body;} private String getnowdat e (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"?>
<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ page import = "java.util. *"%> <% @ Taglib prefix = "HTM" URI = "/ Web-INF / TLDS /HTML-TAG.TLD "%>
<% Locale Loc = New Locale ("fr", "cn"); out.println (Loc.getdisPlayLanguage () ":" Loc.getDisplayCountry () "
"); LOC = New Locale ("fr", "fr"); out.println (Loc.getDisplayLANGuage () ": Loc.getdisplayCountry () "
"); LOC = New Locale (" fr "," Error country code "); Out.Println (Loc.getdisPlayLanguage () ": " Loc.getDisplayCountry () "
");%>
result: