1220227200对应于1980年1月15日(确实是新的日期(1220227200)。ToString ()返回“ Thu Jan 1503:57:07 CET 1970”)。如果向一个日期传递一个长值,即在01/01/1970之前,那么它实际上将返回一个日期01/01/1970。确保您的值不在这种情况下(小于82800000)。
// note: enforcing long literals (L), without it the values would just be wrong.
Date date = new Date(1220227200L * 1000L);
现在,为了正确显示日期,可以使用 java.text. DateFormat,如下所示:
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("Wrong date time value: " + date);
System.out.println("Correct date time value: " + df.format(date));
下面是将转换后的长值显示为 java.util.Date 而不显示的结果
使用日期格式:
Date wrong (off by 2 hours): Mon Sep 01 02:00:00 CEST 2008
Correct date : Monday, 1 September 2008 00:00:00 o'clock UTC
Calendar c = Calendar.getInstance();
c.setTimeInMillis(1385355600000l);
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.DAY_OF_MONTH));
// get Date
System.out.println(c.getTime());
java.time.Instant // Represent a moment as seen in UTC. Internally, a count of nanoseconds since 1970-01-01T00:00Z.
.ofEpochSecond( 1_220_227_200L ) // Pass a count of whole seconds since the same epoch reference of 1970-01-01T00:00Z.
long input = 1_220_227_200L; // Note the "L" appended to long integer literals.
long milliseconds = ( input * 1_000L ); // Use a "long", not the usual "int". Note the appended "L".
long longtime = 1212580300;
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyHHmm");
Date date = (Date) dateFormat.parseObject(longtime + "");
System.out.println(date);
因为1220227200毫秒 = 338,952小时。
Java.util.Date 具有构造函数 new Date (Long milliseconds)-分配一个 Date 对象,并初始化它以表示自标准基准时间“ the epoch”(即1970年1月1日,00:00:00 GMT)以来指定的毫秒数。
所以,在你的情况下,只要记住1秒 = 1000毫秒