java am pm,使用Java 8 DateTime API解析日期和AM / PM

I'm trying to parse a date time string using java 8 DateTime API.

The string to parse contains date, month, year and AM/PM marker such as 17/02/2015 PM.

I use the following pattern: dd/MM/yyyy aa and I expect the parsed LocalDateTime time part to be set at 12:00:00 or 00:00:00 depending on the AM/PM marker.

Using the former java.text.SimpleDateFormat API, the parsing works as expected using this pattern.

But when i use java 8 DateTime API and run the following code:

LocalDateTime.parse("17/02/2015 PM", DateTimeFormatter.ofPattern("dd/MM/yyyy aa"));

I got the following exception:

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)

If i switch the pattern to dd/MM/yyyy a then i got the folowing exception:

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

It's also weird that when i do the reverse operation, format, it works fine:

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")));

respectively prints 17/02/2015 AM and 17/02/2015 PM.

The javadoc for java.time.DateTimeFormatter says (§Resolving, step #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.

What i understand is that the field hour-of-day is mandatory to parse a LocalDateTime... Isn't there any way to parse the time part only using the AM/PM field only?

解决方案

You can set the default values for unavailable fields via 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值