Date Format
public class DateConverter extends DefaultTypeConverter {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public Object convertValue(Map ognlContext, Object value, Class toType) {
Object result = null;
if (toType == Date.class) {
result = doConvertToDate(value);
} else if (toType == String.class) {
result = doConvertToString(value);
}
return result;
}
private Date doConvertToDate(Object value) {
Date result = null;
if (value instanceof String) {
try {
result = sdf.parse((String) value);
} catch (ParseException e) {
throw new XworkException("Could not parse date", e);
}
} else if (value instanceof Object[]) {
// let's try to convert the first element only
Object[] array = (Object[]) value;
if ((array != null) && (array.length >= 1)) {
value = array[0];
result = doConvertToDate(value);
}
} else if (Date.class.isAssignableFrom(value.getClass())) {
result = (Date) value;
}
return result;
}
private String doConvertToString(Object value) {
String result = null;
if (value instanceof Date) {
result = sdf.format(value);
}
return result;
}
}
可以添加默认的xwork-default-conversion.properties
java.util.Date=com.javaeye.core.webwork.converter.DateConverter
也可以写在Domain Model的converter配置里面,具体的内容看webwork文档。
package com.jscud.www.tag;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import com.opensymphony.webwork.views.jsp.WebWorkTagSupport;
/*
* Created Date 2004-12-5
*
*/
/**
* Date的格式化显示,默认格式为yy-MM-dd.
*
* @author scud
*
*/
public class DateShowTag extends WebWorkTagSupport
{
private String pattern;
private String value;
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
Date aDate = null;
Object aValue = findValue(value);
if (null != aValue)
{
aDate = ((Date) aValue);
}
String sRes = "";
if (null != aDate)
{
SimpleDateFormat sdfTemp = new SimpleDateFormat(getPattern());
sRes = sdfTemp.format(aDate).toString();
}
out.print(sRes);
return SKIP_BODY;
}
catch (IOException ex)
{
ex.printStackTrace();
throw new JspException(ex.getMessage());
}
}
public void setPattern(String pattern)
{
this.pattern = pattern;
}
public String getPattern()
{
if (null == pattern)
{
return getDefaultPattern();
}
else
return pattern;
}
public void setValue(String value)
{
this.value = value;
}
public String getDefaultPattern()
{
return "yy-MM-dd";
}
}
java代码: |
package com.jscud.www.tag; /* * Created Date 2004-12-5 * */ /** * DateTime的Tag. * * @author scud * */ public class DateTimeShowTag extends DateShowTag { public String getDefaultPattern() { return "yy-MM-dd HH:mm"; } |
eway 写道: |
在使用webwork过程中遇到了这样一个问题:
我得domain model类包含了一个属性java.util.Date beginTime; webwork使用的model driven的方式。 界面上需要显示格式是:yyyy-MM-dd HH:mm,但是用<ww:property value="beginTime"/>似乎webwork默认的转换格式是yy-MM-dd,怎样才能得到yyyy-MM-dd HH:mm呢? |
webwork cookbook:http://wiki.opensymphony.com/display/WW/How+to+format+dates+and+numbers
定义一个action或者包的properties文件
在里面按照MessageFormat 定义你的date format 比如
java代码: |
format.date={0,date,yyyy-MM-dd HH:mm} |
在jsp页面中用<ww:text>标签
java代码: |
<ww:text name="'format.date'" value0="yourdate" /> |