博客文章:https://www.zjhuiwan.cn/info/20180925/1809251210427830002.html
關(guān)于springmvc接收前臺(tái)傳的時(shí)間類型參數(shù)
前臺(tái)jsp用的一個(gè)日期插件,后臺(tái)獲取一直有問題每窖。
被這個(gè)問題搞了好久肺素,其實(shí)很簡(jiǎn)單粤策。記錄下來壮锻,希望可以幫到遇到同樣問題的同學(xué)琐旁。
我項(xiàng)目使用的ssm框架, 在做web開發(fā)的時(shí)候猜绣,頁面?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)換有問題康铭,
debug發(fā)現(xiàn)前臺(tái)傳過來的是一串?dāng)?shù)字惯退,猜測(cè)應(yīng)該是毫秒,然后就在DateConverter類中將接受的source先進(jìn)行了毫秒轉(zhuǎn)成日期格式的時(shí)間从藤,在進(jìn)行轉(zhuǎn)換結(jié)果沒報(bào)錯(cuò)但日期還是不對(duì)催跪,最后猜測(cè)前臺(tái)傳過來的應(yīng)該是秒,debug將穿過來的日期記下來呛哟,用計(jì)算器轉(zhuǎn)換發(fā)現(xiàn)確實(shí)是秒(這日期插件 --5!扫责!一開始沒想到傳過來的時(shí)間是秒..算是個(gè)坑吧i欢Α)。問題找到了鳖孤,剩下的就是日期轉(zhuǎ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è)問題關(guān)鍵在于前臺(tái)傳過來的居然是秒藏古,搞了半天....