日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC 中设置日期字符串转换格式

發布時間:2025/3/8 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC 中设置日期字符串转换格式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

? ? 在使用SpringMVC的項目中經常遇到?controller 參數中接收Date類型的數據,但是頁面傳過來的參數又是日期字符串,會出現轉換異常。

由于項目需要支持兩種日期格式所以從寫了一個日期轉換器。網友可以根據自己需要實現內部的內容。

package com.its.mmo;import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang.StringUtils;/*** 日期格式話工具* SPRINGMVC日期字符換轉換Date* @author ALLEN*/ public class DateEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text) throws IllegalArgumentException {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = null;try {if(StringUtils.isNotBlank(text)){date = format.parse(text);}} catch (ParseException e) {format = new SimpleDateFormat("yyyy-MM-dd");try {date = format.parse(text);} catch (ParseException e1) {e1.printStackTrace();}}setValue(date);} }

然后在controller 中添加@InitBinder

/*** 初始化日期格式* * @param request* @param binder* @throws Exception*/@InitBinderprotected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {binder.registerCustomEditor(Date.class, new DateEditor());}

?

轉載于:https://my.oschina.net/unteacher/blog/693266

總結

以上是生活随笔為你收集整理的SpringMVC 中设置日期字符串转换格式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。