自定义struts日期显示格式

Struts日期显示格式
一、使用properties配置文件
1.写一个Messages.properties配置文件:
#datetime:
global.format.date={0,date,yyyy-MM-dd}
global.format.time={0,date,HH:mm:ss}
global.format.datetime={0,date,yyyy-MM-dd HH:mm:ss}
global.format.datetime02={0,date,yyyy-MM-dd HH:mm}
#datetime是控制时间格式

2.页面上:<s:text name="global.format.date"><s:param value="deployDate" /></s:text>

其中:<s:text name="global.format.date">控制你要显示的时间格式

<s:param value="deployDate" />是你要显示的时间
<s:text>

二、使用<s:data>标签
<input type="text" value="<s:date name="deployDate" format="yyyy-MM-dd" />" />
其中name="deployDate"是你要显示的时间,format="yyyy-MM-dd"是显示的格式

struts自带的日期格式化,仅支持date类型的数据,因此使用自定义标签扩展此功能
以下为标签主要代码
Java代码 收藏代码
  1. publicbooleanstart(Writerwriter){
  2. booleanresult=super.start(writer);
  3. try{
  4. Datedate=getDateValue();/*获取日期时间对象*/
  5. SIMPLE_DATE_FORMAT.applyPattern(getTargetFormat());/*设置要显示日期格式*/
  6. writer.write(SIMPLE_DATE_FORMAT.format(date));
  7. }catch(Exceptione){
  8. log.error(e.getMessage(),e);
  9. returnfalse;
  10. }
  11. returnresult;
  12. }
  13. privateStringgetTargetFormat(){
  14. targetPattern=TextProviderHelper.getText(targetPattern,"",stack);
  15. if(StringUtils.isEmptyString(targetPattern)){
  16. if("datetime".equals(dateType)){
  17. targetPattern=SelfCareConst.DATE_DDMMMYYYYHH24MMSS;
  18. }else{
  19. targetPattern=SelfCareConst.DATE_DDMMMYYYY;
  20. }
  21. }
  22. returntargetPattern;
  23. }
  24. privateDategetDateValue()throwsParseException{
  25. Objectobj=stack.findValue(value);
  26. if(objinstanceofDate){
  27. return(Date)obj;
  28. }elseif(objinstanceofString||objinstanceofInteger||objinstanceofLong){
  29. /*如果在页面直接写value=“201212”等类似,则此value最终会被转换成Long或者Integer。因此在此判断*/
  30. SIMPLE_DATE_FORMAT.applyPattern(getSrcFormat());
  31. returnSIMPLE_DATE_FORMAT.parse(String.valueOf(obj));
  32. }else{
  33. log.error("unSupportthetypeof["+value+"],thetypeis["+obj.getClass().getName()+"]");
  34. returnnull;
  35. }
  36. }
  37. privateStringgetSrcFormat()
  38. {
  39. srcPattern=TextProviderHelper.getText(srcPattern,"",stack);
  40. if(StringUtils.isEmptyString(srcPattern)){
  41. log.error("Srcpatternisnull.");
  42. }
  43. returnsrcPattern;
  44. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值