具有固定毫位数的java.time ISO日期格式(在Java 8及更高版本中) [英] java.time ISO date format with fixed millis digits (in Java 8 and later)

查看:653
本文介绍了具有固定毫位数的java.time ISO日期格式(在Java 8及更高版本中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下, <$ <$ href =http://docs.oracle.com/javase/8/docs/api/java/time/Instant.html的c $ c> toString 方法=noreferrer> Instant 使用 DateTimeFormatter.ISO_INSTANT formatter。如果格式化程序碰巧为0,则格式化程序不会打印小数位数。



java-time 示例:

  2015-10-08T17:13:07.589Z 
2015-10-08T17:13:07Z

Joda-Time 示例(以及我对java.time的期望):



< pre class =lang-none prettyprint-override> 2015-10-08T17:13:07.589Z
2015-10-08T17:13:07.000Z

在某些系统中解析这真的很令人沮丧。 Elasticsearch 是我遇到的第一个问题,没有预定义的格式支持可选的millis,但我可以使用自定义格式解决这个问题。默认似乎是错误的。



看来你无法为Instants构建自己的格式字符串。是实现我自己的java.time.format.DateTimeFormatterBuilder.InstantPrinterParser的唯一选项吗?

解决方案

只需创建一个 DateTimeFormatter 保留三个小数位。

  DateTimeFormatter formatter = new DateTimeFormatterBuilder()。appendInstant(3).toFormatter(); 

然后使用它。例如:

  System.out.println(formatter.format(Instant.now())); 
System.out.println(formatter.format(Instant.now()。truncatedTo(ChronoUnit.SECONDS)));

...打印(当我运行时):

  2015-10-08T21:26:16.571Z 
2015-10-08T21:26:16.000Z

类文档


... fractionalDigits参数允许输出要控制的小数秒。指定零将导致不输出小数位。从1到9将输出越来越多的数字,必要时使用零右边填充。特殊值-1用于输出必要的数字以避免任何尾随零。 ...



By default, the toString method of Instant uses the DateTimeFormatter.ISO_INSTANT formatter. That formatter won’t print the digits for fraction-of-second if they happen to be 0.

java-time examples:

    2015-10-08T17:13:07.589Z
    2015-10-08T17:13:07Z

Joda-Time examples (and what I'd expect from java.time):

    2015-10-08T17:13:07.589Z
    2015-10-08T17:13:07.000Z

This is really frustrating to parse in some systems. Elasticsearch was the first problem I encountered, there's no pre-defined format that supports optional millis, but I can probably work around that with a custom format. The default just seems wrong.

It appears that you can’t really build your own format string for Instants anyway. Is the only option implementing my own java.time.format.DateTimeFormatterBuilder.InstantPrinterParser?

解决方案

Just create a DateTimeFormatter that keeps three fractional digits.

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendInstant(3).toFormatter();

Then use it. For example:

System.out.println(formatter.format(Instant.now()));
System.out.println(formatter.format(Instant.now().truncatedTo(ChronoUnit.SECONDS)));

…prints (at the time I run it):

2015-10-08T21:26:16.571Z
2015-10-08T21:26:16.000Z

Excerpt of the class doc:

… The fractionalDigits parameter allows the output of the fractional second to be controlled. Specifying zero will cause no fractional digits to be output. From 1 to 9 will output an increasing number of digits, using zero right-padding if necessary. The special value -1 is used to output as many digits as necessary to avoid any trailing zeroes. …

这篇关于具有固定毫位数的java.time ISO日期格式(在Java 8及更高版本中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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