java-Instant.truncatedTo(ChronoUnit.HOURS)返回的值大于原始值
作者:互联网
根据Instant.truncatedTo的javadoc,它所做的是
Truncating the instant returns a copy of the original with fields
smaller than the specified unit set to zero.
因此,如果我使用ChronoUnit.HOURS进行调用,我希望小时不会被修改.当我用1970年以后的日期来调用它时,也看到了这一点.但是,在此之前,它实际上返回的值大于原始值.这是预期的行为还是我错过了什么?
Instant.ofEpochMilli(-1); //1969-12-31T23:59:59.999Z
Instant.ofEpochMilli(-1).truncatedTo(ChronoUnit.HOURS); //1970-01-01T00:00:00Z
Instant.ofEpochMilli(1); //1970-01-01T00:00:00.001Z
Instant.ofEpochMilli(1).truncatedTo(ChronoUnit.HOURS); //1970-01-01T00:00:00Z
Instant.ofEpochMilli(2*60*60*1000).truncatedTo(ChronoUnit.HOURS); //1970-01-01T02:00:00Z
Instant.ofEpochMilli(2*60*60*1000-1); //1970-01-01T01:59:59.999Z
Instant.ofEpochMilli(2*60*60*1000-1).truncatedTo(ChronoUnit.HOURS);//1970-01-01T01:00:00Z
解决方法:
Is this the intended behavior
不,它是Java 9中修复的a bug.
标签:java-time,java 来源: https://codeday.me/bug/20191118/2030954.html