struts使用日期包括将string自动转化为日期fill到form中,以及将form中的日期按照指定格式显示在html的textfield中。首先讲第一种情况的解决方法:
创建如下类:
import java.util.*;
import org.apache.commons.beanutils.*;
import java.text.SimpleDateFormat;
public class DateConvert implements Converter
{
static SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
public DateConvert()
{
}
public Object convert(Class type, Object value)
{
if(value==null)return null;
if(((String)value).trim().length()==0) return null;
if(value instanceof String)
{
try
{
return df.parse((String)value);
}
catch (Exception ex)
{
throw new ConversionException("输入的日期类型非法");
}
}
else
{
throw new ConversionException("输入的不是字符类型");
}
}
}
然后在你的系统某出使用如下(如web的init方法)
ConvertUtils.register(new DateConvert(),java.util.Date.class);
参数用于表示DateConvert类负责处理java.util.Date类型的转化
创建如下类:
import java.util.*;
import org.apache.commons.beanutils.*;
import java.text.SimpleDateFormat;
public class DateConvert implements Converter
{
static SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
public DateConvert()
{
}
public Object convert(Class type, Object value)
{
if(value==null)return null;
if(((String)value).trim().length()==0) return null;
if(value instanceof String)
{
try
{
return df.parse((String)value);
}
catch (Exception ex)
{
throw new ConversionException("输入的日期类型非法");
}
}
else
{
throw new ConversionException("输入的不是字符类型");
}
}
}
然后在你的系统某出使用如下(如web的init方法)
ConvertUtils.register(new DateConvert(),java.util.Date.class);
参数用于表示DateConvert类负责处理java.util.Date类型的转化