java中的日期轉(zhuǎn)換、springmvc接收前臺(tái)的Date類型參數(shù)遇到的坑

博客文章:https://www.zjhuiwan.cn/info/20180925/1809251210427830002.html

關(guān)于springmvc接收前臺(tái)傳的時(shí)間類型參數(shù)

前臺(tái)jsp用的一個(gè)日期插件,后臺(tái)獲取一直有問(wèn)題。

被這個(gè)問(wèn)題搞了好久,其實(shí)很簡(jiǎn)單。記錄下來(lái),希望可以幫到遇到同樣問(wèn)題的同學(xué)。

我項(xiàng)目使用的ssm框架, 在做web開發(fā)的時(shí)候,頁(yè)面?zhèn)魅氲亩际荢tring類型,SpringMVC可以對(duì)一些基本的類型進(jìn)行轉(zhuǎn)換,但是對(duì)于日期類的轉(zhuǎn)換可能就需要我們配置。

1、如果查詢類是我們自己寫,那么在屬性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd")? ,即可將String轉(zhuǎn)換為Date類型,如下

@DateTimeFormat(pattern = "yyyy-MM-dd")

private Date createTime;

2、如果我們只負(fù)責(zé)web層的開發(fā),就需要在controller中加入數(shù)據(jù)綁定:

(```

@InitBinder??

?public?void?initBinder(WebDataBinder?binder)?{??

?????SimpleDateFormat?dateFormat?=?new?SimpleDateFormat("yyyy-MM-dd");??

?????dateFormat.setLenient(false);??

?????binder.registerCustomEditor(Date.class,?new?CustomDateEditor(dateFormat,?true));

?????//true:允許輸入空值,false:不能為空值

```)

3、可以在系統(tǒng)中加入一個(gè)全局類型轉(zhuǎn)換器實(shí)現(xiàn)轉(zhuǎn)換器,新建一個(gè)controller

(```

public?class?DateConverter?implements?Converter<String,?Date>?{????

??@Override????

??public?Date?convert(String?source)?{????

??????SimpleDateFormat?dateFormat?=?new?SimpleDateFormat("yyyy-MM-dd");????

?????dateFormat.setLenient(false);????

??????try?{????

?????????return?dateFormat.parse(source);????

?????}?catch?(ParseException?e)?{????

?????????e.printStackTrace();????

?????}???????????

?????return?null;????

?}

```)


在springmvc配置文件中進(jìn)行配置:

(```

<beanid="conversionService"class="org.springframework.format.support.

FormattingConversionServiceFactoryBean">????

????????<propertyname="converters">????

????????????<list>????

????????????????<beanclass="com.doje.XXX.web.DateConverter"/>????

????????????</list>????

????????</property>????

</bean>?

<mvc:annotation-drivenconversion-service="conversionService"/>

```)

我使用了第三種方式,但在運(yùn)行的時(shí)候報(bào)錯(cuò),最后發(fā)現(xiàn)是DateConverter類中的日期轉(zhuǎn)換有問(wèn)題,

debug發(fā)現(xiàn)前臺(tái)傳過(guò)來(lái)的是一串?dāng)?shù)字,猜測(cè)應(yīng)該是毫秒,然后就在DateConverter類中將接受的source先進(jìn)行了毫秒轉(zhuǎn)成日期格式的時(shí)間,在進(jìn)行轉(zhuǎn)換結(jié)果沒(méi)報(bào)錯(cuò)但日期還是不對(duì),最后猜測(cè)前臺(tái)傳過(guò)來(lái)的應(yīng)該是秒,debug將穿過(guò)來(lái)的日期記下來(lái),用計(jì)算器轉(zhuǎn)換發(fā)現(xiàn)確實(shí)是秒(這日期插件 --!?。∫婚_始沒(méi)想到傳過(guò)來(lái)的時(shí)間是秒..算是個(gè)坑吧?。?。問(wèn)題找到了,剩下的就是日期轉(zhuǎn)換的問(wèn)題了(

java中時(shí)間類型轉(zhuǎn)換

(```

/**

?????*?秒轉(zhuǎn)換為指定格式的日期

?????*?

?????*?@param?second

?????*?@param?patten

?????*?@return?Date類型

?????*?@throws?ParseException

?????*/

????public?synchronized?static?Date?secondToDate(long?second,

?????String?patten)?throws?

????ParseException?{

????????Calendar?calendar?=?Calendar.getInstance();

????????calendar.setTimeInMillis(second?*?1000);//?轉(zhuǎn)換為毫秒

????????Date?date?=?calendar.getTime();

????????SimpleDateFormat?formatter?=?new?SimpleDateFormat(patten);

????????String?dateString?=?formatter.format(date);

????????//?ParsePosition?pos?=?new?ParsePosition(8);

????????Date?currentTime?=?formatter.parse(dateString);

????????return?currentTime;

????}

????/**

?????*?秒轉(zhuǎn)換為指定格式的日期

?????*?

?????*?@param?second

?????*?@param?patten

?????*?@return?String類型

?????*/

????public?synchronized?static?String?secondToDateString(long?second,?

????String?patten)

?????{

????????Calendar?calendar?=?Calendar.getInstance();

????????calendar.setTimeInMillis(second?*?1000);//?轉(zhuǎn)換為毫秒

????????Date?date?=?calendar.getTime();

????????SimpleDateFormat?format?=?new?SimpleDateFormat(patten);

????????String?dateString?=?format.format(date);

????????return?dateString;

????}

????/**

?????*?返回日時(shí)分秒

?????*?

?????*?@param?second

?????*?@return

?????*/

????public?synchronized?static?String?secondToTime(long?second)?{

????????long?days?=?second?/?86400;//?轉(zhuǎn)換天數(shù)

????????second?=?second?%?86400;//?剩余秒數(shù)

????????long?hours?=?second?/?3600;//?轉(zhuǎn)換小時(shí)數(shù)

????????second?=?second?%?3600;//?剩余秒數(shù)

????????long?minutes?=?second?/?60;//?轉(zhuǎn)換分鐘

????????second?=?second?%?60;//?剩余秒數(shù)

????????if?(0?<?days)?{

????????????return?days?+?"天,"?+?hours?+?"小時(shí),"?+minutes?+

?????????????"分,"+second+?"秒";

????????}?else?{

????????????return?hours?+?"小時(shí),"?+?minutes?+?"分,"?+?

????????????second?+?"秒";

????????}

????}

```)


好了,基本就是這樣了。這個(gè)問(wèn)題關(guān)鍵在于前臺(tái)傳過(guò)來(lái)的居然是秒,搞了半天....

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容