问题
Is there a bug is java.util.Date? While doing some testing, I was setting the milliseconds to 2147483647 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
2147483647 9223372036854775807
回答1:
2147483647 milliseconds is only about 24.8 days, so that's why it shows up as 1970-01-25 20:31:23.
4294967295 milliseconds is only about 49.7 days, so that's why it shows up as 1970-02-19 17:02:47.
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.
来源:https://stackoverflow.com/questions/15014589/java-util-date-bug