一个完整的对日期、金额格式化的标签及使用示例

1、完整JAVA类如下:

/* * 创建日期 2009-04-13 * Author:Fenglibin * Blog:http://blog.csdn.net/fenglibing * 更改所生成文件模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */ import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.xpath.XPath; /** * 对金额及日期进行格式化,通过重写其doStartTag方法实现 * @author Administrator * 使用方式 * 更改所生成类型注释的模板为 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */ public class MoneyDateFormat extends TagSupport { private static final long serialVersionUID = 1L; private String format; // 现支持对金额及日期进行格式化,只有值date和money合法[必输] private String xpath;//可根据对指定的xpath的值进行格式化[选输],必须以JDOM的属性值传入,否则这里会报错 private String value;//需要格式化的值(xpath和value必选其一输入)[选输],如这里format指定为money是,可输入任意有效数字;如指定为date的时候,可输入任意有效的日期字符串 private String source;//原数据来源:xpath取自xml结点,string表示直接处理该字符串,这个时候的必须输入value属性,合法的值:xpath和string,可以不输入,默认为string[选输] private String inPattern; //如果是对日期进行格式化,此时的模式,表示要处理的输入的日期字符串对应的格式,如"yyyy-MM-dd" or "yyyy-MM-dd hh:mm:ss" or "yyyy-MM-dd hh:mm:ss:SSS"[选输] private String outPattern; //如果是对日期进行格式化,此时的模式,表示输出的日期对应的模式,如"yyyy-MM-dd" or "yyyy-MM-dd hh:mm:ss" or "yyyy-MM-dd hh:mm:ss:SSS"[选输] private String locale;//本地化,对金额有效[选输] /** * */ public MoneyDateFormat() { super(); // TODO 自动生成构造函数存根 } /* * (非 Javadoc) * * @see javax.servlet.jsp.tagext.Tag#doStartTag() */ public int doStartTag() throws JspException { // TODO 自动生成方法存根 // Document ele=(Document)pageContext.getRequest().getAttribute("result"); Object ele = null; String path=null; if(getXpath()!=null && !getXpath().equals("")){ path=getXpath(); } String source=getSource(); if(source==null || source.equals("")){ source="string"; } //取被处理的值 String value=getValue(); /*******************对金额格式化时,针对不同的国家、语言进行不同的格式(开始)*******************/ Locale thisLocale=null; if(getLocale()==null || getLocale().equalsIgnoreCase("")){//默认当前用户所在地语言 thisLocale=new Locale("","",""); }else if(getLocale().equalsIgnoreCase("SIMPLIFIED_CHINESE") || getLocale().equalsIgnoreCase("CHINA") || getLocale().equalsIgnoreCase("PRC") || getLocale().equalsIgnoreCase("CN")){//简体中文 thisLocale=new Locale("zh", "CN", ""); }else if(getLocale().equalsIgnoreCase("TRADITIONAL_CHINESE") || getLocale().equalsIgnoreCase("TAIWAN") || getLocale().equalsIgnoreCase("TW")){//繁体中文 thisLocale=new Locale("zh", "TW", ""); }else if(getLocale().equalsIgnoreCase("FRANCE") || getLocale().equalsIgnoreCase("FR")){//法国 thisLocale=new Locale("fr", "FR", ""); }else if(getLocale().equalsIgnoreCase("GERMANY") || getLocale().equalsIgnoreCase("DE")){//德国 thisLocale=new Locale("de", "DE", ""); }else if(getLocale().equalsIgnoreCase("ITALY") || getLocale().equalsIgnoreCase("IT")){//意大利 thisLocale=new Locale("it", "IT", ""); }else if(getLocale().equalsIgnoreCase("JAPAN") || getLocale().equalsIgnoreCase("JP")){//日本 thisLocale=new Locale("ja", "JP", ""); }else if(getLocale().equalsIgnoreCase("KOREA") || getLocale().equalsIgnoreCase("KR")){//韩国 thisLocale=new Locale("ko", "KR", ""); }else if(getLocale().equalsIgnoreCase("UK") || getLocale().equalsIgnoreCase("EN")){//英国 thisLocale=new Locale("en", "GB", ""); }else if(getLocale().equalsIgnoreCase("US")){//美国 thisLocale=new Locale("en", "US", ""); }else if(getLocale().equalsIgnoreCase("CANADA") || getLocale().equalsIgnoreCase("CA")){//加拿大英语 thisLocale=new Locale("en", "CA", ""); }else if(getLocale().equalsIgnoreCase("CANADA_FRENCH")){//加拿大法语 thisLocale=new Locale("fr", "CA", ""); }else{//默认当前用户所在地语言 thisLocale=new Locale("","",""); } /*******************对金额格式化时,针对不同的国家、语言进行不同的格式(结束)*******************/ try { // new XMLOutputter().output(ele, pageContext.getOut()); // System.out.println(path); String output = ""; if(source==null || source.equals("") || source.equals("xpath")){ if (path == null) { return SKIP_BODY; } //ele = RootDocument.getDataElement(pageContext); ele=getDataElement(pageContext); if (ele == null) { return SKIP_BODY; } Object node = XPath.selectSingleNode(ele, path); if (node != null) { if (node instanceof Element) { output = ((Element) node).getText(); } else if (node instanceof Attribute) { output = ((Attribute) node).getValue(); } if ("money".equalsIgnoreCase(getFormat())) {//金额格式化 NumberFormat formatter = NumberFormat.getCurrencyInstance(thisLocale); output = formatter.format(Double.parseDouble(output)); } else if ("date".equalsIgnoreCase(getFormat())) {//日期格式化 output = dateToString(stringToDate(value, getInPattern()),getOutPattern()); } } }else if(source!=null && !source.equals("") && source.equals("string")){ if(getValue()==null || getValue().equals("")){ return SKIP_BODY; } if ("money".equalsIgnoreCase(getFormat())) { NumberFormat formatter = NumberFormat.getCurrencyInstance(thisLocale); output = formatter.format(Double.parseDouble(value)); } else if ("date".equalsIgnoreCase(getFormat())) { output = dateToString(stringToDate(value, getInPattern()),getOutPattern()); } } pageContext.getOut().write(output); // pageContext.getOut().write(ele.toString()); } catch (Exception e) { e.printStackTrace(); } return super.doStartTag(); } public static void main(String[] args) throws Exception { /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); System.out.println(sdf.parse("2004-10-10 15:10:10.0")); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH点"); System.out.println(format.format(sdf.parse("2004-10-10 13:10:10.0"))); System.out.println(null + "abc"); */ //当字符串转日期型的时候,format的格式,一定要和需要转成日期的字符串相匹配 //String dateString="2009-01-01",format="yyyy-MM-dd"; String dateString="2009-01-01 01:01:02",format="yyyy-MM-dd hh:mm:ss"; String result=stringToDate(dateString, format).toString(); System.out.println("字符转日期:"+result.toString()); System.out.println("日期转字符串:"+dateToString(stringToDate(dateString, format),"yyyy年MM月dd日 hh点:mm分:ss秒")); } /*******************对定义参数的SET、GET方法定义(开始)*******************/ /** * @return */ public String getFormat() { return format; } /** * @param string */ public void setFormat(String string) { format = string; } public String getLocale() { return locale; } public void setLocale(String locale) { this.locale = locale; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getXpath() { return xpath; } public void setXpath(String xpath) { this.xpath = xpath; } public String getInPattern() { return inPattern; } public void setInPattern(String inPattern) { this.inPattern = inPattern; } public String getOutPattern() { return outPattern; } public void setOutPattern(String outPattern) { this.outPattern = outPattern; } /*******************对定义参数的SET、GET方法定义(结束)*******************/ /** * 将字符型转换为指定格式日期型 * @param _date 需要转换成日期的字符串 * @param format 与需要转换成日期的字符串相匹配的格式 * @return */ private static Date stringToDate(String _date,String format){ if(null == format || "".equals(format)) { format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sdf = new SimpleDateFormat(format); Date date=null; try { date=sdf.parse(_date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } /** * 将日期型转换为指定格式的字符串 * @param date 日期 * @param format 格式 * @return */ public static String dateToString(Date date,String format){ if(null == format || "".equals(format)) { format = "yyyy年MM月dd日 hh点:mm分:ss秒"; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } /** * 取得当前需要以JDOM打包的XML文档 * @param pageContext * @return */ public static Document getDataElement(PageContext pageContext) { Document root = (Document) pageContext.getRequest().getAttribute("xmlDoc"); return root; } }

2、配置test1.tld,放于/WEB-INF/tag/test1.tld:

<?xml version="1.0" encoding="GB2312"?> <!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.0</tlib-version> <jsp-version>1.0</jsp-version> <short-name>sc</short-name> <uri>http://youip.com</uri> <display-name>这里显示你标准的名字</display-name> <description> 对写的标签的描述 </description> <tag> <name>mdf</name> <tag-class>com.sunline.tags.MoneyDateFormat</tag-class> <description> 显示对象的值。 format可以是"money","date"; 对金额与日期进行格式化 对原类com.sunline.tags.Value有类似功能,增加对传入的字符进行格式化,格式的传不一定必须是xpath </description> <attribute> <name>format</name> <required>true</required> </attribute> <attribute> <name>xpath</name> <required>false</required> </attribute> <attribute> <name>value</name> <required>false</required> </attribute> <attribute> <name>source</name> <required>false</required> </attribute> <attribute> <name>inPattern</name> <required>false</required> </attribute> <attribute> <name>outPattern</name> <required>false</required> </attribute> <attribute> <name>locale</name> <required>false</required> </attribute> </tag> </taglib>

3、web.xml中增加配置:

<taglib> <taglib-uri>http://youip.com</taglib-uri> <taglib-location>/WEB-INF/tagtld/test1.tld</taglib-location> </taglib>

4、JSP实例:

<%@ taglib uri="http://youip.com" prefix="sc"%> <sc:mdf format="money" source="string" value="12345678.9"/> <br> <sc:mdf format="money" source="string" value="12345678.9" locale="FRANCE"/> <br> <sc:mdf format="money" source="string" value="12345678.9" locale="US"/> <br> <sc:mdf format="date" source="string" value="2009-01-01" inPattern="yyyy-MM-dd" outPattern="yyyy年-MM月-dd日 hh时:mm分:ss秒" locale="US"/>

5、输入如下:

¤ 12,345,678.90
12345678,90 ?
$12,345,678.90
2009Äê-01ÔÂ-01ÈÕ 12ʱ:00·Ö:00Ãë

其中的乱码是因为我未在JSP中设置字符,不心担心,显示运行成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值