将日期转换为时间戳

时间:2014-05-08 18:00:56

标签: java date timestamp

如何在Java中将日期转换为时间戳?

这一行给出了以下错误: "构造函数Timpestamp(Date)未定义"

Timestamp timestamp = new Timestamp(Date);

2 个答案:

答案 0 :(得分:1)

试试这个:

import java.util.Date;
import java.sql.Timestamp;

public Timestamp convertDateToTimestamp(Date date) {
    Timestamp timestamp = null;
    if (date != null) {
        timestamp = new Timestamp(date.getTime());
    }
    return timestamp;
}

答案 1 :(得分:1)

Timestamp timestamp = new Timestamp(new Date().getTime());

Timestamp timestamp = new Timestamp(date.getTime());