美文网首页
SpringMVC日期类型转换

SpringMVC日期类型转换

作者: 清风欢明月渡 | 来源:发表于2019-08-09 11:29 被阅读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")

相关文章

网友评论

      本文标题:SpringMVC日期类型转换

      本文链接:https://www.haomeiwen.com/subject/htvcjctx.html