Java Current OffsetDateTime object in UTC with three digit fractional seconds (YYYY-MM-dd'T'HH:mm:ss.SSS'Z')

huangapple go评论61阅读模式
英文:

Java Current OffsetDateTime object in UTC with three digit fractional seconds (YYYY-MM-dd'T'HH:mm:ss.SSS'Z')

问题

Sure, here's the translated code snippet:

OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("UTC"));

And here's the translated utility method:

public static OffsetDateTime offsetDateTimeUtc(){
    OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("UTC"));
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    return OffsetDateTime.parse(offsetDateTime.format(formatter));
}

Please note that I've corrected the pattern in the DateTimeFormatter to use lowercase "yyyy" for the year and adjusted the single quotes around the 'T' and 'Z' characters for the desired format.

英文:
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("UTC"));

Above snippet gives me six-digits fractional seconds, but I need a utility method that returns current OffsetDateTime object (not String) in UTC with three-digits format (for example: 2023-05-15T02:22:39.330Z)

Is this possible?
I tried following, and still gives me six-digits fractional seconds.

    public static OffsetDateTime offsetDateTimeUtc(){
        OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("UTC"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
        return OffsetDateTime.parse(offsetDateTime.toString());
    }

答案1

得分: 1

The provided text appears to be code-related and discusses the formatting and rendering of OffsetDateTime objects. Here's the translated content:

上面的片段给我六位小数秒

这是不正确的。OffsetDateTime 表示给定偏移量的时间,它 不表示该时间的任何特定呈现。因此,上面的片段(指的是创建 OffsetDateTime 对象)会给您一个 OffsetDateTime 对象。它本身没有 任何 呈现。因此,说它具有六位小数秒呈现是不正确的。实际上,它有 9 位小数秒。您想要这 9 位中的多少取决于在呈现时所调用的方法。它的 toString() 显示 6 位,我认为是这样。您无法更改这个

返回当前的 OffsetDateTime 对象(而不是字符串),以三位数字格式表示在 UTC 中

因此,这是不可能的。OffsetDateTime 对象不包含有关其呈现的任何相关信息。您正在将 ODT 对象传递给的代码具有自己的呈现器(通常是一个 DateTimeFormatter 对象),您需要更改该对象。如果无法更改它,那么您没有其他选择。可能其他代码正在调用 toString() - 这会使用某个未指定的任意呈现器(DateTimeFormatter 对象) - 除非文档明确规定它呈现了什么(很少见),或者用于调试目的。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");

是的,这个格式化程序将给您 3 位数字。

return OffsetDateTime.parse(offsetDateTime.toString());

不,这没有完成任何事情。这样做会:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
System.out.println(formatter.format(offsetDateTime));
英文:

> Above snippet gives me six-digits fractional seconds

This is incorrect. an OffsetDateTime represents a time at a given offset, it does not represent any particular rendering of that time. Hence, the above snippet (Referring to the creation of an OffsetDateTime object) gives you.. an OffsetDateTime object. It does not inherently have any rendering. It is therefore incorrect to say it has a six-digit fractional second rendering. It doesn't, actually, it has 9. How many of those 9 you want depends on the call you make when rendering it. Its toString() shows 6, I believe. You cannot change this.

> returns current OffsetDateTime object (not String) in UTC with three-digits format

Hence, this is impossible. An OffsetDateTime object does not contain any relevant info about its rendering. The code you are passing your ODT object to has its own renderer (usually, a DateTimeFormatter object), and you'd have to change that object. If you can't do that, you're out of options. Possibly the other code is calling toString() - that uses some unspecified arbitrary renderer (DateTimeFormatter object) - you should never use toString() unless the docs explicitly spec out what it renders (rare), or for debugging purposes.

> DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");

Yes, this formatter will get you 3 digits.

> return OffsetDateTime.parse(offsetDateTime.toString());

No, this doesn't accomplish anything. This would:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
System.out.println(formatter.format(offsetDateTime));

答案2

得分: 0

以下是翻译好的部分:

实际上,以下内容有效。

public static OffsetDateTime offsetDateTimeUtc(){
        OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("UTC"));
        return OffsetDateTime.parse(offsetDateTime.format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'")));
    }
英文:

Actually, following worked.

public static OffsetDateTime offsetDateTimeUtc(){
        OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.of("UTC"));
        return OffsetDateTime.parse(offsetDateTime.format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'")));
    }

huangapple
  • 本文由 发表于 2023年5月15日 10:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定