Java - LocalDateTime解析日期字符串值时丢弃秒值“00“,如“2021-07-01 15:33:00“ 转换为“2021-07-01T15:33“

Posted Zhang Daopin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java - LocalDateTime解析日期字符串值时丢弃秒值“00“,如“2021-07-01 15:33:00“ 转换为“2021-07-01T15:33“相关的知识,希望对你有一定的参考价值。

    在测试数据的时候,收集到各个时间点上的数据,发现怎么数据有点奇怪,都是中间的时间,没有整秒的数据呢?

    查看后台日志,发现是因为在使用LocalDateTime接收秒值为"00"的数据时,秒值被丢弃掉了 

 wrong date and time parameter format, parameter: 2021-07-01T15:33, format requirement: [yyyy-MM-ddTHH:mm:ss | yyyy-MM-ddTHH:mm:ss.SSS][+HH:mm | -HH:mm | Z], position: character 157, line 1"}

错误的日期和时间参数格式,参数:21-07-01 t15:33,格式要求:[yyyy-MM-ddTHH:mm:ss | yyyy-MM-ddTHH:mm:ss。[+HH:mm | -HH:mm | Z],位置:字符157,第1行"} 

    但是原来的数据是:"2021-07-01 15:33:00" 而不是:"2021-07-01T15:33"

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@52adeeec] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2fcac179] will not be managed by Spring
==>  Preparing: insert into test_table(`token`,`session_id`, `link`,`cid`, `link_type`,`detail_id`, `xxx_id`, `source`, `xxx_link`, `page_open`, `page_click`, `length_of_stay`, `xxx_time`, `ip`, `os`, `xxx`, `created`, `address`, `xxx_id`, `xxx_name`, `xxxr_id`, `xxxe_id`) values (?,?,?,?,?,?,?,?,? ,?,?,?,?,?,?,?,?,?,? ,?,?,?)
==> Parameters: null(String), sessionId(String), A06zeA05(String), 1(Integer), (String), -1(Long), null, http://zhangdaopin.localhost.com/AAAABBBB(String), null, true(Boolean), null, null, 2021-07-01 15:33:00.0(Timestamp), 172.18.24.64(String), Windows(String), Chrome 8(String), 2021-07-01 15:33:46.306(Timestamp), 未分配或者内网IP(String), aaaaaaaaaaaaaaaaaaaaaaa(String), null(String), null(String), null(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@52adeeec]
[WARN ] 2021-07-01 15:33:46,487 method:com.xxxxx.xx.xxxxapi.sdk.client.DefaultxxxxapiHttpSdk.getBody(DefaultxxxxapiHttpSdk.kt:32)
Request failed: {"timestamp":"2021-07-01T07:33:44.512Z","code":400,"status":"FAILED_PRECONDITION","message":"Parameter error, wrong date and time parameter format, parameter: 2021-07-01T15:33, format requirement: [yyyy-MM-ddTHH:mm:ss | yyyy-MM-ddTHH:mm:ss.SSS][+HH:mm | -HH:mm | Z], position: character 157, line 1","details":[{"message":"Parameter error, wrong date and time parameter format, parameter: 2021-07-01T15:33, format requirement: [yyyy-MM-ddTHH:mm:ss | yyyy-MM-ddTHH:mm:ss.SSS][+HH:mm | -HH:mm | Z], position: character 157, line 1"}],"module":"xxxx-service","service":"xxxxapi","error_code":"152204","msg":"Parameter error, wrong date and time parameter format, parameter: 2021-07-01T15:33, format requirement: [yyyy-MM-ddTHH:mm:ss | yyyy-MM-ddTHH:mm:ss.SSS][+HH:mm | -HH:mm | Z], position: character 157, line 1"}
[INFO ] 2021-07-01 15:33:46,528 method:com.xxxx.channel.task.AutoCountTask.processHits(AutoCountTask.java:169)
统计数据命令执行出现异常SdkException
com.xxxxx.xx.sdk.exception.SdkException: Parameter error, wrong date and time parameter format, parameter: 2021-07-01T15:33, format requirement: [yyyy-MM-ddTHH:mm:ss | yyyy-MM-ddTHH:mm:ss.SSS][+HH:mm | -HH:mm | Z], position: character 157, line 1
        at com.xxxxx.xx.xxxxapi.sdk.client.DefaultxxxxapiHttpSdk.getBody(DefaultxxxxapiHttpSdk.kt:41)
        at com.xxxxx.xx.xxxxapi.sdk.client.DefaultxxxxapiHttpSdk.execute(DefaultxxxxapiHttpSdk.kt:55)
        at com.xxxxx.channel.service.xxxxapiService.xxxxApiHttpService.senxxsgToxxxxapi(xxxxApiHttpService.java:63)
        at com.xxxxx.channel.service.ContentVisitTrackService.lambda$saveVisitTrack$1(ContentVisitTrackService.java:186)
        at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
        at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:647)
        at com.xxxxx.channel.service.ContentVisitTrackService.saveVisitTrack(ContentVisitTrackService.java:184)
        at com.xxxxx.channel.task.AutoCountTask.processHits(AutoCountTask.java:134)
        at com.xxxxx.channel.task.AutoCountTask.run(AutoCountTask.java:57)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:853)

    第一种办法可以在字段上增加格式化的指定

@JSONField(format = "yyyy-MM-dd'T'HH:mm:ss")

    第二种办法可以在写库的时候,格式化这个日期

LocalDateTime visitTime = LocalDateTime.new();
String visitTimeFormat = visitTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));

以上是关于Java - LocalDateTime解析日期字符串值时丢弃秒值“00“,如“2021-07-01 15:33:00“ 转换为“2021-07-01T15:33“的主要内容,如果未能解决你的问题,请参考以下文章

Java - LocalDateTime解析日期字符串值时丢弃秒值“00“,如“2021-07-01 15:33:00“ 转换为“2021-07-01T15:33“

Java - LocalDateTime解析日期字符串值时丢弃秒值“00“,如“2021-07-01 15:33:00“ 转换为“2021-07-01T15:33“

Spring boot + Jackson + LocalDateTime:日期解析不正确

LocalDateTime 以数组格式表示

Java经典实例:把字符串解析为日期时间

Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date