(简单)DateFormat,允许24:00:00和00:00:00作为输入 [英] (Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

查看:202
本文介绍了(简单)DateFormat,允许24:00:00和00:00:00作为输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找了一段时间,到目前为止没有成功.您是否知道是否有一个"DateFormat" ish类,该类可以让我使用"00:00:00"和"24:00:00"作为输入参数(它们都是午夜),但是当被称为"getHour()"时结果我将得到0或24?

I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result?

使用"kk"仅允许我具有< 1:24>范围,同时我正在寻找< 0:24>范围格式

Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting

推荐答案

24:00不在LocalTime中表示,因为它严格属于第二天.考虑了可以将24:00表示为LocalTime一部分的模型,但结论是,在许多用例中,这将非常令人困惑,并且会产生超出其解决范围的错误.

The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to models where 24:00 could be represented as part of LocalTime, but the conclusion was that it would be very confusing in a lot of use cases, and create more bugs than it solves.

但是java.time中支持24:00.完全可以使用标准格式技术对其进行解析,但是有必要使用SMART或LENIENT模式,请参见

There is support for 24:00 in java.time however. It is perfectly possible to parse it using the standard formatting techniques, however it is necessary to use SMART or LENIENT mode, see ResolverStyle. The default mode is SMART, however the formtter constants on DateTimeFormatter like DateTimeFormatter.ISO_LOCAL_DATE_TIME are in STRICT mode. Thus, ofPattern() defaults to SMART mode:

static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");

LocalDateTime ldt = LocalDateTime.parse("2012-12-03T24:00", FORMATTER);
System.out.println(ldt);  // 2012-12-04T00:00

请注意,这也适用于OffsetDateTimeZonedDateTime. Instant的标准解析器无需特殊格式化程序即可支持24:00:

Note that this also works for OffsetDateTime and ZonedDateTime. The standard parser for Instant supports 24:00 without a special formatter:

Instant instant = Instant.parse("2015-01-01T24:00:00Z");
System.out.println(instant);  // 2015-01-02T00:00:00Z

任何格式化程序都可以使用

Any formatter can be converted to SMART or LENIENT mode using withResolverStyle() as follows:

DateTimeFormatter f = ...  // obtain a formatter somehow
DateTimeFormatter smartMode = f.withResolverStyle(ResolverStyle.SMART);

// for example
f = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withResolverStyle(ResolverStyle.SMART);


第二个支持元素是


The second element of support is parseExcessDays(). This allows the excess day to be obtained when only the time is being parsed:

static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");

TemporalAccessor parsed = TIME_FORMATTER.parse("24:00");
LocalTime lt = LocalTime.from(parsed);
Period excessDays = parsed.query(DateTimeFormatter.parsedExcessDays());
System.out.println(lt + " + " + excessDays);  // 00:00 + P1D


最后,是给高级用户的注释.从理论上讲,应该可以编写自己的Temporal实现,该实现是LocalTime的副本,但支持24:00作为有效值.这样的类(例如LocalTimeWithEndOfDay)可以毫无问题地与格式化程序/解析器一起使用(并且可以很好地添加到


Finally, a note for advanced users. It should in theory be possible to write your own implementation of Temporal that is a copy of LocalTime but with support for 24:00 as a valid value. Such a class, say LocalTimeWithEndOfDay, could then operate with the formatter/parser without issue (and might make a good addition to ThreeTen-Extra.

这篇关于(简单)DateFormat,允许24:00:00和00:00:00作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆