Arya07(Java日期时间的问题)

java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。

第一个构造函数使用当前日期和时间来初始化对象:Date( )
第二个构造函数接收一个参数,该参数是从1970年1月1日起的毫秒数:Date(long millisec)。
之前将时间存进数据库的时候,第一个表的时间是现在时间开始算的,第二个表就是1970年开始的,代码如下:
第一个表的:  Date curTime = new Date();
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
             String regtime = sdf.format(curTime);

第二个表的:Date time=new Date(1);
           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置成mysql能识别的datetime形式
           String time1=sdf.format(time);

这两个时间的区别就是第二个Date:
public Date(long date)
Constructs a Date object using the given millisecondstime value. If the given milliseconds value contains timeinformation, the driver will set the time components to thetime in the default time zone (the time zone of the Java virtualmachine running the application) that corresponds to zero GMT.
Parameters:date - milliseconds since January 1, 1970, 00:00:00 GMT notto exceed the milliseconds representation for the year 8099.A negative number indicates the number of millisecondsbefore January 1, 1970, 00:00:00 GMT.

你可以对我进行打赏哦