Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為L(zhǎng)ocalDateTime出現(xiàn)的問題
Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為L(zhǎng)ocalDateTime出現(xiàn)的問題
問題復(fù)現(xiàn)
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-03-12' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2020-03-12 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at demo.LocalDateTimeUtils.parseString(LocalDateTimeUtils.java:22)
at demo.DateTimeDemo.main(DateTimeDemo.java:12)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2020-03-12 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 3 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2020-03-12 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 5 more
問題解決
解決方案:先將文本字符串日期轉(zhuǎn)化為L(zhǎng)ocalDate類型,再將LocalDate轉(zhuǎn)化為L(zhǎng)ocalDateTime
LocalDateTimeUtils.parseStringToLocalDateTime(
couponForm.getStartTime(), DateTimeFormatPatternConstants.YYYY_MM_DD)
/**
* 解析字符串為日期
* <p>
* 說明:
* 1. 該方法主要解決yyyy-MM-dd格式文本字符串無法直接轉(zhuǎn)換為L(zhǎng)ocalDateTime的問題
*
* @param source 需要解析的日期字符串
* @param formatPattern 日期格式化模式
* @return 格式化后的日期
*/
public static LocalDateTime parseStringToLocalDateTime(String source, String formatPattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern);
LocalDate localDate = LocalDate.parse(source, dateTimeFormatter);
return localDate.atStartOfDay();
}
java.time.LocalDate.atStartOfDay()方法將此日期與午夜時(shí)間組合在一起,以便在此日期開始時(shí)創(chuàng)建LocalDateTime
到此這篇關(guān)于Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為L(zhǎng)ocalDateTime出現(xiàn)的問題的文章就介紹到這了,更多相關(guān)SpringBoot yyyy-MM-dd轉(zhuǎn)換為L(zhǎng)ocalDateTime內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA與模擬器安裝調(diào)試失敗的處理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES
這篇文章主要介紹了IDEA與模擬器安裝調(diào)試失敗的處理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java兩個(gè)List<T> 求交集,差集,并集,去重后的并集
本文主要介紹了Java兩個(gè)List<T> 求交集,差集,并集,去重后的并集,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例
這篇文章主要為大家分享了java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Java的面向?qū)ο缶幊袒靖拍顚W(xué)習(xí)筆記整理
這篇文章主要介紹了Java的面向?qū)ο缶幊袒靖拍顚W(xué)習(xí)筆記整理,包括類與方法以及多態(tài)等支持面向?qū)ο笳Z言中的重要特點(diǎn),需要的朋友可以參考下2016-01-01
idea使用easyCode生成代碼(根據(jù)mybatis-plus模板創(chuàng)建自己的模板)
本文主要介紹了idea使用easyCode生成代碼,easyCode代碼生成器可以減少低價(jià)值搬磚,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解
這篇文章主要介紹了Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解,BeanFactory及其子類是Spring IOC容器中最重要的一個(gè)類,BeanFactory由類名可以看出其是一個(gè)Bean工廠類,其實(shí)它確實(shí)是一個(gè)Bean工廠類,完成Bean的初始化操作,需要的朋友可以參考下2023-12-12
Java中的CyclicBarrier循環(huán)柵欄詳解
這篇文章主要介紹了Java中的CyclicBarrier循環(huán)柵欄詳解,CyclicBarrier循環(huán)柵欄是用來進(jìn)行線程協(xié)作,等待線程滿足某個(gè)計(jì)數(shù),構(gòu)造時(shí)設(shè)置計(jì)數(shù)個(gè)數(shù),每個(gè)線程執(zhí)行到某個(gè)需要“同步”的時(shí)刻調(diào)用 await()方法進(jìn)行等待,當(dāng)?shù)却木€程數(shù)滿足計(jì)數(shù)個(gè)數(shù)時(shí),繼續(xù)執(zhí)行,需要的朋友可以參考下2023-12-12

