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类型的数据,因此使用自定义标签扩展此功能
以下为标签主要代码
一、使用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类型的数据,因此使用自定义标签扩展此功能
以下为标签主要代码
- publicbooleanstart(Writerwriter){
- booleanresult=super.start(writer);
- try{
- Datedate=getDateValue();/*获取日期时间对象*/
- SIMPLE_DATE_FORMAT.applyPattern(getTargetFormat());/*设置要显示日期格式*/
- writer.write(SIMPLE_DATE_FORMAT.format(date));
- }catch(Exceptione){
- log.error(e.getMessage(),e);
- returnfalse;
- }
- returnresult;
- }
- privateStringgetTargetFormat(){
- targetPattern=TextProviderHelper.getText(targetPattern,"",stack);
- if(StringUtils.isEmptyString(targetPattern)){
- if("datetime".equals(dateType)){
- targetPattern=SelfCareConst.DATE_DDMMMYYYYHH24MMSS;
- }else{
- targetPattern=SelfCareConst.DATE_DDMMMYYYY;
- }
- }
- returntargetPattern;
- }
- privateDategetDateValue()throwsParseException{
- Objectobj=stack.findValue(value);
- if(objinstanceofDate){
- return(Date)obj;
- }elseif(objinstanceofString||objinstanceofInteger||objinstanceofLong){
- /*如果在页面直接写value=“201212”等类似,则此value最终会被转换成Long或者Integer。因此在此判断*/
- SIMPLE_DATE_FORMAT.applyPattern(getSrcFormat());
- returnSIMPLE_DATE_FORMAT.parse(String.valueOf(obj));
- }else{
- log.error("unSupportthetypeof["+value+"],thetypeis["+obj.getClass().getName()+"]");
- returnnull;
- }
- }
- privateStringgetSrcFormat()
- {
- srcPattern=TextProviderHelper.getText(srcPattern,"",stack);
- if(StringUtils.isEmptyString(srcPattern)){
- log.error("Srcpatternisnull.");
- }
- returnsrcPattern;
- }