[SSM-SpringMVC Chapter] 06-Date format conversion, back-end Date to json, front-end json to Date

1. SpringMVC string to date format conversion

  SpringMVC does not support the conversion of date strings on the page (similar to 2020-10-14 and 2020/10/14) to the background Date.

1.1 @DateTimeFormat()Realize date format conversion through annotations . 【 *** Recommended Use】

  Annotation date type converter (add a note to the Date attribute that needs to be converted to date format):@DateTimeFormat("需要转换的格式")

 出生日期<input type="date" name="birthday"/><br/>
public User{
    
    
private int id;
  private String username;
  private double password;
  //注解将yyyy-MM-dd的形式转换为Date数据,具体什么格式要看传入参数格式,就是发送请求,查看请求参数的表单数据,看birthday数据的格式
  @DateTimeFormat(pattern = "yyyy-MM-dd")
  private Date birthday;
}

Check the request header and find that the format of the incoming date is a
yyyy-MM-ddformat, depending on the request information.Insert picture description here



1.2 Write a custom converter implementation Converter, and perform type conversion by rewriting methods.

1.2.1 Write a custom converter to implement the Converterinterface
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

 /*自定义类型参数转换器: 将字符串日期格式转成Date类型的数据.
      Converter<S,T>  S: 代表的是源,将要转换的数据类型  T:目标类型,将会转成什么数据类型
 */
 //日期格式转换器
public class DateFormatConverter implements Converter<String,Date>{
    
    
  //转换方法
  //String source 将会传递过来的日期的字符串
  public Date convert(String source) {
    
    
   //参数填写为你想要转换的数据格式
​    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");try {
    
    
​      Date date = sdf.parse(source);return date;} catch (ParseException e) {
    
    
​      e.printStackTrace();}return null;
  }
1.2.2 Configure the conversion factory in springmvc.xml and set the converter to the converters collection
<bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><set><bean id="dateFormatConverter" class="com.itheima.converter.DateTimeFormatConverter"></bean></set></property>
  </bean>
1.2.3 Mount the conversion factory object to the processor adapter ( springmvc.xml)
		<!-- 这个service的值要和你创建的转换器的bean的id一致 -->
		<mvc:annotation-driven conversion-service="formattingConversionService"/>


2. SpringMVC date formatting tags, date to the specified format conversion (used in jsp)

2.1 Need jstl dependency

		<dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

2.2 Import tags in the jsp page

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

2.3 Call date formatting tag fmtlabel (Import the prefix you set)

<fmt:formatDate value="${User.birthday}" pattern="yyyy-MM-dd-"/>
In this way, the date data format you pass in the backend is yyyy-MM-dd format

2.4 Just add a @DateTimeFormat(pattern = "yyyy-MM-dd")comment on the date in the background

//这个pattern需要和你传入的数据格式一致
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthDay;

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/109053413