- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 java 8 DateTime API 解析日期时间字符串。
要解析的字符串包含日期、月份、年份和 AM/PM 标记,例如 17/02/2015 PM
。我使用以下模式:dd/MM/yyyy aa
并且我希望将解析的 LocalDateTime 时间部分设置为 12:00:00
或 00:00 :00
取决于 AM/PM 标记。
使用以前的 java.text.SimpleDateFormat
API,解析按使用此模式的预期进行。
但是当我使用 java 8 DateTime API 并运行以下代码时:
LocalDateTime.parse("17/02/2015 PM", DateTimeFormatter.ofPattern("dd/MM/yyyy aa"));
我得到以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: Too many pattern letters: a
at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1765)
at java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1604)
at java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1572)
at java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:534)
如果我将模式切换为 dd/MM/yyyy a
然后我得到以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '17/02/2015 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.LocalDateTime$$Lambda$7/474675244.queryFrom(Unknown Source)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 5 more
同样奇怪的是,当我进行反向操作时,格式化,它工作正常:
System.out.println(LocalDateTime.of(2015, 02, 17, 0, 0, 0).format(DateTimeFormatter.ofPattern("dd/MM/yyyy a")));
System.out.println(LocalDateTime.of(2015, 02, 17, 12, 0, 0).format(DateTimeFormatter.ofPattern("dd/MM/yyyy a")));
分别打印17/02/2015 AM
和17/02/2015 PM
。
java.time.DateTimeFormatter
的 javadoc 说(§Resolving,步骤 #6):
- A LocalTime is formed if there is at least an hour-of-day available. This involves providing default values for minute, second and fraction of second.
我的理解是,字段 hour-of-day 是解析 LocalDateTime 所必需的...难道没有任何方法可以仅使用 AM/PM 字段来解析时间部分吗?
最佳答案
您可以通过 DateTimeFormatterBuilder.parseDefaulting
设置不可用字段的默认值:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("dd/MM/yyyy a")
.parseDefaulting(ChronoField.HOUR_OF_AMPM, 0) // this is required
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) // optional, but you can set other value
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) // optional as well
.toFormatter();
System.out.println(LocalDateTime.parse("17/02/2015 PM", formatter)); // 2015-02-17T12:00
System.out.println(LocalDateTime.parse("17/02/2015 AM", formatter)); // 2015-02-17T00:00
关于java - 使用 java 8 DateTime API 解析日期和 AM/PM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32180044/
我正在尝试使用以下方法对 datetime.datetime 对象列表求和: from datetime import datetime, timedelta d= [datetime.datetim
我正在尝试这个 (datetime.datetime.today()-datetime.datetime.today()).days 给出 -1 并期待值 0 而不是我得到 -1。在这种情况下,我将结
如果我列一个时间增量的列表,平均值比我对这些增量的微秒值求平均时要大。为什么会这样呢?。赠送。这是Linux上的Python3.8.10。
考虑以下片段: import datetime print(datetime.datetime.now() - datetime.datetime.now()) 在 x86_64 Linux 下的 P
如何在 SQLAlchemy 查询中比较 DateTime 字段和 datetime.datetime 对象? 例如,如果我这样做 candidates = session.query(User).f
我收到以下错误: type object 'datetime.datetime' has no attribute 'datetime' 在下面一行: date = datetime.datetime
尝试找出如何将当前日期锁定为变量,以从输入的 self.birthday 中减去。我已经查看了各种示例和链接,但无济于事......建议? from datetime import datetime
您好,我有一些 datetime.datetime 格式的日期,我用它们来过滤带有 Pandas 时间戳的 Pandas 数据框。我刚刚尝试了以下方法并获得了 2 小时的偏移量: from datet
如果您调用 datetime.datetime.now(datetime.timezone.utc) 您会得到类似 datetime.datetime(2021, 9, 8, 1, 33, 19, 6
我正在使用 pywin32 读取/写入 Excel 文件。我在 Excel 中有一些日期,以 yyyy-mm-dd hh:mm:ss 格式存储。我想将它们作为 datetime.datetime 对象
据我所知,自 Unix 纪元(1970-01-01 00:00:00 UTC)以来的秒数在全局各地应该是相同的,因为它固定为 UTC。 现在,如果您所在的时区有几个小时 +/- UTC,为什么这样做会
我正在尝试添加 datetime.datetime 和 datetime.time 以获得一列。我正在尝试结合: import datetime as dt dt.datetime.combine(m
我有一个脚本需要在脚本的不同行执行以下操作: today_date = datetime.date.today() date_time = datetime.strp(date_time_string
我在 AppEngine 上收到 type object 'datetime.datetime' has no attribute 'datetime' 错误,提示日期时间类型,但我的导入是 impo
所以我一直在使用 python 语言制作东西。我遇到了一些不太容易理解的错误: TypeError: 'datetime.datetime' object is not subscriptable (
当我运行时 from datetime import date, time, timedelta date(2012, 11, 1) + timedelta(0, 3600) 结果是 datetime
我的目标是转换 utc进入loc : use chrono::{Local, UTC, TimeZone}; let utc = chrono::UTC::now(); let loc = chron
假设您有一个 datetime.date 对象,例如 datetime.date.today() 返回的对象。 稍后您还会得到一个表示时间的字符串,它补充了日期对象。 在 datetime.datet
我试过了 In [16]: import datetime In [17]: now = datetime.datetime.utcnow() In [18]: isinstance(now, dat
我有以下代码并且收到上述错误。由于我是 python 新手,因此无法理解此处的语法以及如何修复错误: if not start or date < start: start = date 最佳答案 有
我是一名优秀的程序员,十分优秀!