[英]Java OffsetDateTime.parse() adds extra zeros to time
This following codes return an OffsetDateTime like this 2021-06-30T23:59:59.009966667Z, with 2 extra zeros added.以下代码返回一个 OffsetDateTime,如 2021-06-30T23:59:59.009966667Z,添加了 2 个额外的零。 I have 7 n's in the formatter, but it still returns 9 digits.我在格式化程序中有 7 个 n,但它仍然返回 9 个数字。 Why?为什么?
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class HelloWorld{
public static void main(String []args){
DateTimeFormatter MAIN_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnn XXX");
String dbData = "2021-06-30 23:59:59.9966667 +00:00";
OffsetDateTime time = OffsetDateTime.parse(dbData, MAIN_FORMATTER);
System.out.println(time.toString());
}
}
This following codes return an OffsetDateTime like this 2021-06-30T23:59:59.009966667Z, with 2 extra zeros added.以下代码返回一个 OffsetDateTime,如 2021-06-30T23:59:59.009966667Z,添加了 2 个额外的零。 I have 7 n's in the formatter, but it still returns 9 digits.我在格式化程序中有 7 个 n,但它仍然返回 9 个数字。 Why?为什么?
You will get the same result even for a single n
or any number of n
s less than or equal to 7
.即使对于单个n
或任何数量小于或等于7
的n
,您也会得到相同的结果。 However, if you exceed 7
, you will get an exception something like但是,如果您超过7
,您将收到类似的异常
Exception in thread "main" java.time.format.DateTimeParseException:
Text '2021-06-30 23:59:59.9966667 +00:00' could not be parsed at index 20
The reason for this is DateTimeFormatter
counts the number of n
in the pattern and if it is less than or equal to the number of digits after .
这样做的原因是DateTimeFormatter
计算模式中n
的数量,如果它小于或等于之后的位数.
, it will use additional n
to compensate that but if the number of n
s exceed the number of digits, it won't have any clue what that additional n
s are for. ,它将使用额外的n
来补偿,但如果n
的数量超过位数,它将不知道额外的n
的用途。
It's not just for n
but for most (if not all) of the symbols.它不仅适用于n
,而且适用于大多数(如果不是全部)符号。 You can understand it from the following example:您可以从以下示例中理解它:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDate1 = "2020/2/2";
String strDate2 = "2020/02/02";
// Notice the single M and d
DateTimeFormatter dtfCorrectForBothDateStrings = DateTimeFormatter.ofPattern("uuuu/M/d", Locale.ENGLISH);
System.out.println(LocalDate.parse(strDate2, dtfCorrectForBothDateStrings));
System.out.println(LocalDate.parse(strDate1, dtfCorrectForBothDateStrings));
// Notice the two M's and d's
DateTimeFormatter dtfCorrectOnlyForSecondDateString = DateTimeFormatter.ofPattern("uuuu/MM/dd", Locale.ENGLISH);
System.out.println(LocalDate.parse(strDate2, dtfCorrectOnlyForSecondDateString));
System.out.println(LocalDate.parse(strDate1, dtfCorrectOnlyForSecondDateString));
}
}
Output: Output:
2020-02-02
2020-02-02
2020-02-02
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020/2/2' could not be parsed at index 5
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1953)
at java.base/java.time.LocalDate.parse(LocalDate.java:429)
at Main.main(Main.java:16)
.009966667
and not .9966667
?为什么它给我.009966667
而不是.9966667
? The symbol, n
stands for nano-of-second and 9966667
nanoseconds are equal to 0.009966667
second.符号n
代表纳秒, 9966667
纳秒等于0.009966667
秒。
public class Main {
public static void main(String[] args) {
int nanos = 9966667;
System.out.println(nanos + " nanoseconds = " + ((double) nanos / 1_000_000_000) + " second");
}
}
Output: Output:
9966667 nanoseconds = 0.009966667 second
S
?它如何与S
一起工作? S
stands for fraction-of-second and .9966667
is parsed as 0.9966667
second. S
代表秒的分数, .9966667
被解析为0.9966667
秒。
OffsetDateTime#toString
implementation:关于OffsetDateTime#toString
实现的注释: The OffsetDateTime#toString
groups the digits of fraction-of-second in the nearest multiple of 3
(ie milli, micro and nano) eg OffsetDateTime#toString
以最接近3
的倍数(即毫、微和纳米)对秒的小数位数进行分组,例如
.99
, the output will be .990
, and对于.99
, output 将为.990
,并且.996
, the output will be .996
, and对于.996
, output 将为.996
,并且.9966
, the output will be .996600
, and对于.9966
, output 将为.996600
,并且.9966667
, the output will be .996666700
.对于.9966667
, output 将为.996666700
。 Given below is an excerpt from OffsetDateTime#toString
:下面给出了OffsetDateTime#toString
的摘录:
The output will be one of the following ISO-8601 formats: output 将是以下 ISO-8601 格式之一:
- uuuu-MM-dd'T'HH:mmXXXXX uuuu-MM-dd'T'HH:mmXXXXXX
- uuuu-MM-dd'T'HH:mm:ssXXXXX uuuu-MM-dd'T'HH:mm:ssXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX uuuu-MM-dd'T'HH:mm:ss.SSSXXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX
The final demo (incorporating all that have been discussed above):最后的演示(包含上面讨论的所有内容):
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2021-06-30 23:59:59.9966667 +00:00";
System.out.println(OffsetDateTime.parse(strDateTime,
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnn XXX", Locale.ENGLISH)));
System.out.println(OffsetDateTime.parse(strDateTime,
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS XXX", Locale.ENGLISH)));
}
}
Output: Output:
2021-06-30T23:59:59.009966667Z
2021-06-30T23:59:59.996666700Z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.