根据日期获取long值
select UNIX_TIMESTAMP('2018-03-01 00:00:00')
- 运行结果: 1519833600
根据long值获取日期
使用默认格式
select FROM_UNIXTIME(1519833600);
- 运行结果: 2018-03-01 00:00:00
使用自定义格式
select FROM_UNIXTIME(1519833600, '%Y-%m-%d %H:%i:%s');
- 运行结果: 2018-03-01 00:00:00
使用long值构造Date
public static void main(String[] args) {
Date date = new Date(1519833600000L);
System.out.println(date);
}
- 运行结果
Thu Mar 01 00:00:00 CST 2018
- 使用sql语句获取的是秒数,而创建Date对象需要的是毫秒数,所以需要乘以1000
/**
* Allocates a <code>Date</code> object and initializes it to
* represent the specified number of milliseconds since the
* standard base time known as "the epoch", namely January 1,
* 1970, 00:00:00 GMT.
*
* @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
* @see java.lang.System#currentTimeMillis()
*/
public Date(long date) {
fastTime = date;
}