将数据库中的日期转换成字符串(框架不能帮我们转换的)在页面上显示,可以利用工具类(有局部的和全局的)
局部的日期转换工具类
package com.itheima.ssm.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
//日期转换为字符串
public static String dateToString(Date date,String patt){
SimpleDateFormat sdf=new SimpleDateFormat(patt);
String format=sdf.format(date);
return format;
}
//字符串转换为日期
public static Date stringToDate(String str, String patt) throws Exception {
SimpleDateFormat sdf=new SimpleDateFormat(patt);
Date parse=sdf.parse(str);
return parse;
}
}
在需要日期转换的实体类中使用
Product.java
package com.itheima.ssm.domain;
import com.itheima.ssm.utils.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
public class Product implements Serializable {
private Integer id; // 主键
private String idStr;
private String productNum; // 编号 唯一
private String productName; // 名称
private String cityName; // 出发城市
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date departureTime; // 出发时间
private String departureTimeStr;
private double productPrice; // 产品价格
private String productDesc; // 产品描述
private Integer productStatus; // 状态 0 关闭 1 开启
private String productStatusStr;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIdStr() {
return idStr;
}
public void setIdStr(String idStr) {
this.idStr = idStr;
}
public String getProductNum() {
return productNum;
}
public void setProductNum(String productNum) {
this.productNum = productNum;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Date getDepartureTime() {
return departureTime;
}
public void setDepartureTime(Date departureTime) {
this.departureTime = departureTime;
}
public String getDepartureTimeStr() {
if(departureTime!=null){
//将日期转换为字符串
departureTimeStr= DateUtils.dateToString(departureTime,"yyyy-MM-dd HH:mm");
}
return departureTimeStr;
}
public void setDepartureTimeStr(String departureTimeStr) {
this.departureTimeStr = departureTimeStr;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public Integer getProductStatus() {
return productStatus;
}
public void setProductStatus(Integer productStatus) {
this.productStatus = productStatus;
}
public String getProductStatusStr() {
//因为页面上获取的是ProductStatusStr,
// 而数据库中获取的是ProductStatus,所以要对他进行处理
if (productStatus!=null){
// 状态 0 关闭 1 开启
if(productStatus==0){
productStatusStr="关闭";
}
if (productStatus == 1) {
productStatusStr="开启";
}
}
return productStatusStr;
}
public void setProductStatusStr(String productStatusStr) {
this.productStatusStr = productStatusStr;
}
}
也可以使用全局的工具类,需要实现Converter< T>接口