“ DateTimeFormatter”可在小数秒内处理FULL STOP或COMMA [英] `DateTimeFormatter` to handle either a FULL STOP or COMMA in fractional second

查看:160
本文介绍了“ DateTimeFormatter”可在小数秒内处理FULL STOP或COMMA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.time 类,我们如何为 DateTimeFormatter 允许完全停止()(句点或点)或逗号()作为小数秒的定界符?

In the java.time classes, how do we specify a formatting pattern for a DateTimeFormatter that allows for either a FULL STOP (.) (period or dot) or a COMMA (,) as the delimiter of a fractional second?

例如,以下内容用于解析输入日期在 20090813145607.0Z .0Z ,0Z 结尾的值>和 20090813145607,0Z

For example, the following works for parsing an input date value ending in either .0Z or ,0Z in 20090813145607.0Z and 20090813145607,0Z.

String input = "20090813145607.0Z";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuuMMddHHmmss[,S][.S]X" );

但是对于打印,输出中包含 both 分数秒。

But for printing, the output contains both, generating a repeated pair of fractional seconds.


20090813145607,0.0Z

20090813145607,0.0Z

所以我推断出我在格式化模式中使用 [,S] [。S] 不是获得此功能的正确方法。

So I deduce I my use of [,S][.S] in the formatting pattern is not the correct way to get this functionality.

我尝试使用 DateTimeFormatter :: withDecimalStyle 方法传递 DecimalStyle ,但这无法达到我在实验中预期的效果。该文档并没有真正解释其预期的行为。

I tried to use the DateTimeFormatter::withDecimalStyle method passing a DecimalStyle, but that failed to behave as I expected in my experiments. The documentation does not really explain its intended behavior.

此问题作为 ISO 8601 标准建议使用COMMA,但允许完全停止。

This issue is important as the ISO 8601 standard recommends using the COMMA but allows the FULL STOP. Either is commonly used in practice.

也许在十进制标记中,逗号和/或点的这种容忍度是不可能的java.time。

Perhaps this tolerance of comma and/or dot as decimal mark is not possible in java.time.

我在Java 8 Update 102中尝试了以下代码。带有FULL STOP(点)的第一个输入成功,而带有COMMA的第二个输入失败。

I tried the following code with Java 8 Update 102. The first input with a FULL STOP (dot) succeeds while the second input with a COMMA fails.

String inputDot = "2016-01-02T12:34:56.7Z";  // Succeeds.
Instant instantDot = Instant.parse ( inputDot );

String inputComma = "2016-01-02T12:34:56,7Z";  // Fails. Throws a DateTimeParseException.
Instant instantComma = Instant.parse ( inputComma );


推荐答案

在点与逗号之间,点比以我的经验逗号。 RFC3339 仅与 XML模式。点不再是首选(根据维基百科):

On dot vs comma, dots are far more common than commas in my experience. RFC3339 uses a dot only as does XML schema. The dot is no longer "preferred" (according to wikipedia):


小数点,逗号或点(无优先权,如2003年第22届大会CGPM第10号决议中的
所述) ,[16],但根据ISO优先选择逗号
8601:2004)

A decimal mark, either a comma or a dot (without any preference as stated in resolution 10 of the 22nd General Conference CGPM in 2003,[16] but with a preference for a comma according to ISO 8601:2004)

鉴于此,JSR -310更喜欢点。

Given all this, JSR-310 prefers a dot.

DecimalStyle 类确实提供了一些控制,其中 DateTimeFormatterBuilder.appendFraction 方法与 true 一起使用以输出中的小数点DecimalStyle

The DecimalStyle class does provide some control, where the DateTimeFormatterBuilder.appendFraction method is used with true to output the decimal point from DecimalStyle.

解析点或逗号是不可能的。跟踪为 JDK-8132536 ,它解决了通用的或概念

To parse either a dot or comma is not possible. This is tracked as JDK-8132536, which tackles a general "or" concept in parsing.

使用 ISO_INSTANT 表示未使用本地化的十进制样式。

Instants are parsed using ISO_INSTANT which says that "The localized decimal style is not used".

因此,JSR-310的格式化程序无法执行您想要的操作。

Thus, the formatters of JSR-310 cannot do what you want.

这篇关于“ DateTimeFormatter”可在小数秒内处理FULL STOP或COMMA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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