SpringMVC日期类型转换

2019-08-09  本文已影响0人  清风欢明月渡

1.String转Date

package com.gowiny.myclass.articlecat.data;

import java.text.SimpleDateFormat;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

@ControllerAdvice
public class StringToDateConverter {
    
    @InitBinder
    public void initBinderDateType(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");        
        binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
    }
}

2.Date转String

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
上一篇 下一篇

猜你喜欢

热点阅读