说明:spring
有的时候由前台jsp页面填写一个日期,提交到后台spring mvc的时候,咱们但愿直接转换成一个Date类型,而不是由一个string 类型接收,而后再经过simpleDateFormat来进行转格式,这样太麻烦了,代码会显的很乱,spring为咱们提供了类型转化器,写起来也是很麻烦,咱们的需求很简单就是由框架帮咱们去自动的转换类型而不是手动的转换,在这样的背景下,咱们能够使用@DateTimeFormat注解。此外咱们还有一个需求就是咱们从数据库里面查询到了日期,而后咱们想把这个日期自动的变成string类型,这时咱们能够使用@JsonFormat注解。数据库
@DateTimeFormat @JsonFormat 这两个注解在网上搜索很不少资料,我只提几个须要注意的地方:mvc
1 @DateTimeFormat(pattern="yyyy-MM-dd") 2 private Date alertDate; 3 4 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm") 5 public Date getAlertDate() { 6 return alertDate; 7 } 8 9 public void setAlertDate(Date alertDate) { 10 this.alertDate = alertDate; 11 }