简体   繁体   English

Java 8 ZonedDateTime

[英]Java 8 ZonedDateTime

I am trying to use ZonedDateTime.now(ZoneOffset.UTC).toString() to get the current time. 我正在尝试使用ZonedDateTime.now(ZoneOffset.UTC).toString()来获取当前时间。 It works well, but seems to cut off seconds / milliseconds when they are right on a rollover (00.000Z), or so it appears. 它运作良好,但是当它们恰好在翻转(00.000Z)时似乎缩短了几秒/毫秒,所以它似乎出现了。 Normally it wouldn't matter, but I am writing the data to ElasticSearch, which has a strict mapping for the date I am passing (acquired time). 通常没关系,但是我正在将数据写入ElasticSearch,该数据库对我经过的日期(获取的时间)具有严格的映射。

I maybe am off in what I am thinking is happening, but want to try to make sure I can always provide a date/time with seconds and milliseconds. 我可能不知道正在发生的事情,但是想要尝试确保我始终可以提供带有秒和毫秒的日期/时间。

Basic Code snippet (first pass was to add milliseconds, but appears seconds could also possibly be left out): 基本代码段(第一遍是添加毫秒,但似乎还可能忽略了秒):

def utcDate = ZonedDateTime.now(ZoneOffset.UTC)
def utcDateStr = utcDate.toString()
utcDateStr = utcDateStr.contains(".") ? utcDateStr
        : utcDateStr.replace("Z","").concat(".000Z")

Want it to be in the format below, which it almost always is: 希望它采用以下格式,几乎总是这样:

2016-06-16T05:43:07.624Z

Firstly, it sounds like you really want an Instant rather than a ZonedDateTime - that takes the time zone part out of the picture entirely. 首先,听起来您真的想要一个Instant而不是ZonedDateTime ,它使时区完全脱离了画面。 But either way, just use a DateTimeFormatter to format the value however you want. 但是无论哪种方式,只要使用DateTimeFormatter即可格式化所需的值。

It sounds like you want something like (in Java; adjust to Groovy appropriately): 听起来您想要这样的东西(在Java中;适当地调整为Groovy):

DateTimeFormatter formatter = DateTimeFormatter
    .ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
    .withZone(ZoneOffset.UTC);
Instant now = Instant.now();
String text = formatter.format(now); 
System.out.println(text);

That will still output the milliseconds value even if it's 0. 即使它是0,仍然会输出毫秒值。

(If you really want to use ZonedDateTime , you can do so with the same formatter. But conceptually, what I think you're trying to represent is just an instant in time, and Instant is the better fit for that.) (如果您确实想使用ZonedDateTime ,则可以使用相同的格式化程序来使用。但是从概念上讲,我认为您要表示的只是时间上的即时,而Instant更适合ZonedDateTime 。)

If you want to go the formatter route, you can build one like so: 如果您想使用格式化程序,可以这样构建:

DateTimeFormatter ES_ISO8601_FORMATTER = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .append(
                    new DateTimeFormatterBuilder()
                            .parseCaseInsensitive()
                            .append(ISO_LOCAL_DATE)
                            .appendLiteral('T')
                            .append(
                                    new DateTimeFormatterBuilder()
                                            .appendValue(HOUR_OF_DAY, 2)
                                            .appendLiteral(':')
                                            .appendValue(MINUTE_OF_HOUR, 2)
                                            .appendLiteral(':')
                                            .appendValue(SECOND_OF_MINUTE, 2)
                                            .appendFraction(NANO_OF_SECOND, 3, 9, true)
                                            .toFormatter()
                            )
                            .toFormatter()
            )
            .appendOffsetId()
            .toFormatter();

(That's cobbled together from the code in the DateTimeFormatter class in Java 8) (这与Java 8中DateTimeFormatter类中的代码结合在一起)

The change is basically giving 改变基本上就是

                                        .appendFraction(NANO_OF_SECOND, 3, 9, true)

a min width of 3, instead of 0 in the original ISO formatter 最小宽度3,而不是原始ISO格式程序中的0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM