<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.sfbf.util.DateFormatter"></bean>
</set>
</property>
</bean>
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
public class DateFormatter implements Formatter<Date> {
@Override
public String print(Date arg0, Locale arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public Date parse(String text, Locale locale) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
if(!"".equals(text)&&null!=text)
date = format.parse(text);
} catch (Exception e) {
format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(text);
}
return date;
}
}
第二种方法:可以直接在方法上添加注解 @ResponseBody 返回JSON数据,如果javabean的属性中包含 Date日期类型的数据: