dan*_*ilo 1 java truncate localdatetime
我正在尝试从UTC时区截断毫秒。
我有下面的代码,我可以删除毫秒,但Z最后我仍然得到。
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC );
OffsetDateTime eventDateTime=now.minus(4, ChronoUnit.MINUTES);
System.out.println("====Event date Time before truncate===");
System.out.println(eventDateTime);
System.out.println("====Event date Time after truncate===");
System.out.println(eventDateTime.truncatedTo(ChronoUnit.SECONDS));
Run Code Online (Sandbox Code Playgroud)
输出如下:
====事件日期截断前的时间===
2021-03-09T20:46:24.081Z====事件日期截断后的时间===
2021-03-09T20:46:24Z
要表示 UTC 中的时刻,请使用Instantclass 而不是LocalDateTime或OffsetDateTime。
Instant\n .now() // Returns a `Instant` representing the current moment including a fractional second.\n .truncatedTo( \n ChronoUnit.SECONDS // Granularity of what you want chopped off. In this case, we lop off any fractional second.\n ) // Returns another `Instant` object. Per immutable objects, a new object is instantiated rather than altering ("mutating") the original.\n .minus( // Go backwards on the timeline.\n Duration.ofMinutes( 4 ) // Amount of time to go backwards.\n ) // Returns another `Instant` object. \n .toString() // Generate text in standard ISO 8601 format, with `Z` on the end meaning an offset of zero hours-minutes-seconds from the UTC temporal prime meridian. \nRun Code Online (Sandbox Code Playgroud)\n最后的Z代表重要信息:日期时间代表与 UTC 零小时、分钟、秒的偏移量所看到的时刻。该字母被定义为 ISO 8601 标准格式的一部分。根据航空/军事传统,该字母发音为 \xe2\x80\x9cZulu\xe2\x80\x9d。
如果您不关心问题域中的偏移量或时区,那么您使用的是错误的类型。
\nInstant、OffsetDateTime和类型ZonedDateTime都代表一个时刻,即时间线上的特定点。这三个都涉及偏移量或时区。
LocalDateTime如果您只想要一个包含一天中的时间但缺少偏移量/区域上下文的日期,请使用LocalDateTime. 因为不涉及偏移量,所以Z在表示 a 值的文本中不会出现No。LocalDateTime
请注意,LocalDateTime对象本质上是模糊的,并且不代表某个时刻。换句话说,打电话LocalDateTime.now几乎从来都不是正确的做法。但如果你坚持:
LocalDateTime ldt = LocalDateTime.now( ZoneOffset.UTC ) ;\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2936 次 |
| 最近记录: |