java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案
這是同事遇到的一個(gè)問題。
代碼中的Date,放到頁面上的格式為“Fri Mar 21 09:20:38 CST 2014”(不顯示,只為傳遞到下一個(gè)controller),
再次提交表單時(shí),Dto類的 private Date startTime; 沒有被set進(jìn)值。
用本地程序做了一下實(shí)驗(yàn)
public static void main(String[] args) { Date now = new Date(); System.out.println(now); String nowStr = now.toString(); DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); Date parsedNow = null; try { parsedNow = format.parse(nowStr); System.out.println(parsedNow); } catch (ParseException e) { e.printStackTrace(); } }
程序執(zhí)行format.parse(nowStr)時(shí)報(bào)錯(cuò)
Java.text.ParseException: Unparseable date: "Fri Mar 21 09:25:48 CST 2014"
at java.text.DateFormat.parse(DateFormat.java:337)
分析和查看源碼后得出結(jié)論,由系統(tǒng)所使用的語言導(dǎo)致的錯(cuò)誤。
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
默認(rèn)其實(shí)是
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", new Locale(System.getProperty("user.language")));
其中System.getProperty("user.language")由于系統(tǒng)是中文,所以為zh,應(yīng)該是中文時(shí)區(qū)不支持此種format。
修改上面的代碼驗(yàn)證此觀點(diǎn)
public static void main(String[] args) { Date now = new Date(); System.out.println(now); String nowStr = now.toString(); DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", new Locale(System.getProperty("user.language"))); System.out.println(System.getProperty("user.language")); Date parsedNow = null; try { parsedNow = format.parse(nowStr); System.out.println(parsedNow); } catch (ParseException e) { format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH); try { System.out.println("new format by 'en'"); System.out.println(format.parse(nowStr)); } catch (ParseException e1) { e1.printStackTrace(); } } }
另一種解決方案是,在jsp頁面中,對日期格式進(jìn)行一次轉(zhuǎn)換,如
<input type="hidden" name="data" value=' <fmt:formatDate value="${dto.date}" pattern="yyyy-MM-dd"/> '/>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Spring中的BeanFactory對象實(shí)例化工廠詳解
這篇文章主要介紹了Spring中的BeanFactory對象實(shí)例化工廠詳解,BeanFactory及其子類是Spring IOC容器中最重要的一個(gè)類,BeanFactory由類名可以看出其是一個(gè)Bean工廠類,其實(shí)它確實(shí)是一個(gè)Bean工廠類,完成Bean的初始化操作,需要的朋友可以參考下2023-12-12MyBatis-Plus實(shí)現(xiàn)字段自動(dòng)填充功能的示例
本文主要介紹了MyBatis-Plus實(shí)現(xiàn)字段自動(dòng)填充功能的示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Java服務(wù)中的大文件上傳和下載優(yōu)化技巧分享
在Java服務(wù)中處理大文件的上傳和下載是一項(xiàng)常見但復(fù)雜的任務(wù),為了提供優(yōu)秀的用戶體驗(yàn)和高效的系統(tǒng)性能,我們將探索多種策略和技術(shù),并在每一點(diǎn)上都提供代碼示例以便實(shí)戰(zhàn)應(yīng)用,需要的朋友可以參考下2023-10-10SpringBoot集成kafka全面實(shí)戰(zhàn)記錄
在實(shí)際開發(fā)中,我們可能有這樣的需求,應(yīng)用A從TopicA獲取到消息,經(jīng)過處理后轉(zhuǎn)發(fā)到TopicB,再由應(yīng)用B監(jiān)聽處理消息,即一個(gè)應(yīng)用處理完成后將該消息轉(zhuǎn)發(fā)至其他應(yīng)用,完成消息的轉(zhuǎn)發(fā),這篇文章主要介紹了SpringBoot集成kafka全面實(shí)戰(zhàn),需要的朋友可以參考下2021-11-11POST方法給@RequestBody傳參數(shù)失敗的解決及原因分析
這篇文章主要介紹了POST方法給@RequestBody傳參數(shù)失敗的解決及原因分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10MyBatis 中 ${}和 #{}的正確使用方法(千萬不要亂用)
這篇文章主要介紹了MyBatis 中 ${}和 #{}的正確使用方法,本文給大家提到了MyBatis 中 ${}和 #{}的區(qū)別,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07