Date类的概述和常用方法

Date类的概述和常用方法

Date:表示特定的瞬间,精确到毫秒,可以通过方法来设定自己所表示的时间,可以表示任意的时间

System.currentTimeMillis( ):返回的是当前系统时间,1970-1-1至今的毫秒数

构造方法:

Date( ):创建的是一个表示当前系统时间的Date对象

例如:Date d = new Date( );

Date(long date):根据“指定时间”创建Date对象

例如:Date d = new Date(1000*60*60);

Date的常用方法:

 1.毫秒值--->Date

void setTime(long time)

例如;

Date d = new Date();
d.setTime(1000*60*60);

 [注]:1000*60*60表示一个小时。1000毫秒等于1秒

2.Date--->毫秒值

long getTime( )

Date d = new Date();
long t = d.getTime();

例如:1000*60*60*24*2 = 172800000毫秒(即2天)

Date d = new Date();
d.setTime(172800000);
System.out.println(d.toLocaleString());
System.out.println(d.getTime());
执行结果为:1970-1-3   8:00:00     

                      172800000

如果修改d.setTime(0);

则执行结果为:1970-1-1  8:00:00

                       0

猜你喜欢

转载自blog.csdn.net/weixin_44706512/article/details/89453676