gpt4 book ai didi

JAVA new Timestamp() 使以毫秒为单位的时间以零结尾时结尾零消失

转载 作者:行者123 更新时间:2023-11-30 06:48:43 25 4
gpt4 key购买 nike

在我的日志中,我使用:(java.sql.Timestamp)

 new Timestamp(System.currentTimeMillis())

它的行为很奇怪(恕我直言):

  • 当 System.currentTimeMillis() = 1490713735960 时

    it returns : 2017-03-28 17:08:55.96

我预计:2017-03-28 17:08:55.960 结尾为 0

  • 当 System.currentTimeMillis() = 1490713724721 时

    it returns : 2017-03-28 17:08:44.721

    如预期

问题:

这是 new Timestamp() 的正常行为吗?

为什么终止零被删除?

最佳答案

所以,是的,这是正确的行为。因为纳秒部分的toString()方法的实现是:

if (nanos == 0) {
nanosString = "0";
} else {
nanosString = Integer.toString(nanos);
...
}

else部分:

// Truncate trailing zeros
char[] nanosChar = new char[nanosString.length()];
nanosString.getChars(0, nanosString.length(), nanosChar, 0);
int truncIndex = 8;
while (nanosChar[truncIndex] == '0') {
truncIndex--;
}

nanosString = new String(nanosChar, 0, truncIndex + 1);

查看java.sql.Timestamp.toString()方法的源代码

关于JAVA new Timestamp() 使以毫秒为单位的时间以零结尾时结尾零消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43227495/

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