@DateTimeFormat(pattern="yyyy-MM-dd")
返回的时候java.util.Date
pattern="yyyy-MM-dd"必须要和页面中的日期格式对应。
1、TimeController.java
package com.chenk.web.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.chenk.pojo.Users; @Controller
public class TimeController {
@RequestMapping("/addUserController")
public String addUser(Users user,Model model){
model.addAttribute("user", user);
System.out.println(user);
return "showUser";
}
}
2.User.java
package com.chenk.pojo; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class Users {
private String name;
private String age;
//备用yyyy-MM-dd HH:mm:ss
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Users [name=" + name + ", age=" + age + ", birth=" + birth + "]";
} }
3、页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>日期</title> </head>
<body>
<form action="addUserController" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="text" name="birth" />
<input type="submit" value="提交">
</form>
</body>
</html>