问题描述
java.util.Date是否有错误?在进行一些测试时,我将毫秒设置为
2147483647,这应该给我一个2038-01-19 03:14:07的日期,但它返回1970-01-25 20:31:23。也尝试了4294967295并得到1970-02-19 17:02:47的错误答案。有没有人知道一项工作
Is there a bug is java.util.Date? While doing some testing, I was setting the milliseconds to2147483647 which should give me a date of 2038-01-19 03:14:07 but it is returning 1970-01-25 20:31:23. Also tried it with 4294967295 and get the wrong answer of 1970-02-19 17:02:47. Does anyone know of a work around
import java.util.*;
import java.io.*;
/*
centos 6.3 x64
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
*/
public class Test
{
static public void main(String args[])
{
long ms = 2147483647L;
java.util.Date d = new java.util.Date( ms );
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
// SHOULD be 2038-01-19 03:14:07
System.out.println( df.format( d ) );
System.out.println(df.format(new Date(Long.MAX_VALUE)));
System.out.println( "\n" + ms );
System.out.println( Long.MAX_VALUE );
}
}
1970-01-25 20:31:23
292278994-08-17 07:12:55
1970-01-25 20:31:23292278994-08-17 07:12:55
2147483647
9223372036854775807
21474836479223372036854775807
推荐答案
2147483647毫秒只有大约24.8天,所以这就是为什么它显示为1970-01-25 20:31:23。
2147483647 milliseconds is only about 24.8 days, so that's why it shows up as 1970-01-25 20:31:23.
4294967295毫秒仅约49.7天,因此它显示为1970-02-19 17:02:47。
4294967295 milliseconds is only about 49.7 days, so that's why it shows up as 1970-02-19 17:02:47.
您可能正在计算基于2147483647秒而不是毫秒的预期值。这不是java.util.Date中的错误;通过乘以1000将秒值转换为毫秒。
You may be calculating what you expect based on 2147483647 seconds instead of milliseconds. This is not a bug in java.util.Date; convert your seconds value to milliseconds by multiplying by 1000.
这篇关于java.util.date bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!