gpt4 book ai didi

java - DateTimeFormatter.ISO_INSTANT 毫秒级问题

转载 作者:行者123 更新时间:2023-12-01 10:06:21 31 4
gpt4 key购买 nike

我正在使用 DateTimeFormatter,但在 999 毫秒后的毫秒结束时,它会丢失毫秒字段。如果 java 存在此问题,请告诉我

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("/users/test/filenamedate12.txt");

// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bw= null;
FileWriter fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
for(int i=0;i<=1000000;i++){
ZonedDateTime zdt = ZonedDateTime.now();
String content =zdt.format(DateTimeFormatter.ISO_INSTANT);


//String content = "This is the content to write into create file";


bw.write(content);
bw.newLine();



}

bw.flush();
bw.close();
}

输出样本

2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:56.999Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z
2016-04-05T22:32:57Z

最佳答案

不,这是故意的。来自 DateTimeFormatter.ISO_INSTANT 的 javadoc:

The nano-of-second outputs zero, three, six or nine digits digits as necessary.

换句话说,为了简洁起见,删除了 . 之后的冗余 0

源代码显示:

ISO_INSTANT = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendInstant()
.toFormatter(ResolverStyle.STRICT, null);

appendInstant 方法负责此行为。该方法的文档也提到了它。

关于java - DateTimeFormatter.ISO_INSTANT 毫秒级问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438373/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com