JAVA常用API:Date类
Date类是用来操作日期和时间的类,需要导包import java.util.Date;
Date类有很多方法都过时了,但以下方法,有必要记住。
构造方法
1. date(); 返回日期对象
Date date = new Date(); 将系统当前时间实例化成一个日期对象
2. Date date = new Date(long timeStamp); 将时间戳转成日期对象。所谓时间戳:自1970年0时0分0秒开始,到某个时间点的毫秒数。
二、Date类的方法
1. getTime();返回时间戳,已过时
Date date = new Date();
date.getTime(); 由日期对象调用,返回该日期对象的时间戳。日期转时间戳
2. setTime(long 时间戳); 将时间戳转日期对象。
Date date = new Date();
date.setTime(1528441032861); 时间戳转日期对象
所以将时间戳转日期对象,有两种方法:1利用构造方法,2. 利用setTime
将日期对象转时间戳,也有两种方法:1利用getTime(), 2.利用System.currentTimeMillis()
浙公网安备 33010602011771号