java8解析带有 AM/PM 的12小时制 时间字符串
今天遇到了这样的一件事,时间字段是12小时制的,而且是带有AM/PM 的字符串,怎么解析成标准化的时间格式呢?
时间数据样本:
12/25/2020 11:27:49 AM
经过一番探索后发现代码可以这样写:
DateTimeFormatter df = DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm:ss a",Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.parse(str, df);
System.out.println(dateTime);
测试AM:
测试PM:
解析过程中报错:
java.time.format.DateTimeParseException: Text '12/25/2020 11:27:49 PM' could not be parsed: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 11:27:49at 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 Abc.testDateParse(Abc.java:82)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.time.DateTimeException: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 11:27:49at java.time.format.Parsed.crossCheck(Parsed.java:647)at java.time.format.Parsed.crossCheck(Parsed.java:627)at java.time.format.Parsed.resolve(Parsed.java:246)at java.time.format.DateTimeParseContext.toResolved(DateTimeParseContext.java:331)at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1955)at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)... 24 more
当你看到这个错误的时候,就说明是DateTimeFormatter.ofPattern
用错了,
因为MM/dd/yyyy H:mm:ss a
中大写H是24小时制的,这里要解析的数据是12小时制的,所以这里应该是小写的h,正确写法是MM/dd/yyyy h:mm:ss a
java8解析带有 AM/PM 的12小时制 时间字符串相关推荐
- 【Java】将输入的 24 小时制时间转换为 12 小时制时间
一.题目描述 题目内容: 编写一个程序,要求用户输入24小时制的时间,然后输出显示12小时制的时间. 输入格式: 在一行中输入带有中间的:符号(半角的冒号)的24小时制的时间,小时和分钟均采用2位数字 ...
- js实现12小时制时间
条件: 在页面里动态显示日期与时间 1.小时按12小时制,不足两位的前面补0 2.根据获取的时间,判断上下午,用am,pm表示 代码: <!DOCTYPE html> <html&g ...
- java 将当前日期转换为 24 小时制时间字符串
按指定格式获取时间 package com.primecredit;import java.text.SimpleDateFormat; import java.util.Date;public cl ...
- 24小时制时间格式和12小时制时间格式
yyyy-MM-dd HH:mm:ss -------24 yyyy-MM-dd hh:mm:ss -------12 转载于:https://www.cnblogs.com/hbhzz/p/3 ...
- Splunk中12小时制AM/PM的日期转换
在Splunk中对带有AM/PM的12小时制的日期格式转换成unix time时按照如下格式: | basesearch.... | eval test_time="23/02/2022 0 ...
- 24小时制与12小时制的换算
24小时制时间 12小时制时间 0点 12 am 0-11 x am 12点 12pm 13-23 (x-12)pm 转载于:https://www.cnblogs.com/time-is-life/ ...
- android calendar 24小时制,Android 使用SimpleDateFormat以及Calendar转换时间为12小时制和24小时制时间格式...
由于项目需要,有些地方要使用12小时制时间格式,而有些地方使用的24小时制时间格式 通过研究代码,原来...So easy,现将实现的方法写下来和大家分享 使用SimpleDateFormat时格式化 ...
- 时间类的12小时制输出
Problem F: 时间类的12小时制输出 Time Limit: 4 Sec Memory Limit: 128 MB Submit: 6559 Solved: 3588 [Submit][Sta ...
- IOS 字符串转时间时12小时制24小时制的处理。
如果手机设置的是12小时制时间(hh),而转换时间和格式为24小时制(HH),那么得到的时间为nil 所以要判断当前手机时间格式再转换 下面两种情况获得的date 都为nil //1.如果手机是12 ...
最新文章
- 学习笔记 Keras:基于Python的深度学习库
- linux批量管理服务,通过PSSH批量管理Linux服务器
- .NET Core 2.1中的HttpClientFactory最佳实践
- 前端学习(2474):页面布局
- LeetCode MySQL 1571. 仓库经理
- 容器编排技术 -- Kubernetes kubectl create secret 命令详解
- pythonbyte转int_Python将byte数组转换为int详解
- type traits
- Android中GridView实现互相添加和删除
- Linux进程相关的一些笔记
- C++实现离散数学之真值表(试着自写头文件)
- linux 卸载lxde,UBUNTU最小化搭建LXDE桌面环境
- 【opencv4.3.0教程】04之基础结构及其常用功能介绍1
- GUI设计和UI设计有什么区别?
- 工作复盘_GRAI复盘法
- ORACLE EBS中附件功能的使用
- Springboot idea 导包
- vue比较完整的方法介绍 和获取事件对象
- 一个大概是被称作为“废物”的程序员的回顾,从18岁到30岁
- emWin—显示汉字