springmvc mybatis怎么把mysql的时间格式化

2025-05-07 09:40:05
推荐回答(1个)
回答1:

方法一:实体类中加日期格式化注解
[java] view plaincopy
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date receiveAppTime;
如上,在对应的属性上,加上指定日期格式的注解,本人亲自测试过,轻松解决问题!

需要 import org.springframework.format.annotation.DateTimeFormat;
转换函数位于spring-context.jar包中

方法二:控制器Action中加入一段数据绑定代码
[java] view plaincopy
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); //true:允许输入空值,false:不能为空值