即这段代码
startDate = new Date(timestampValue.getTime));
给我 :
2012-16-02 05:16:17
什么时候
System.out.println(timestampValue);
返回 :
2012-01-02 05:16:17.0
类java.sql.TimeStamp
从java.util.Date
.
您可以直接分配一个TimeStamp
对象来Date
引用:
TimeStamp timeStamp = //whatever value you have;
Date startDate = timestampValue;
您应该改用日历:
Calendar start = Calendar.getInstance();
start.setTimeInMillis( timeStampValue.getTime() );
花哨的新 Java 8 方式是Date.from(timestamp.toInstant())
. 请参阅我在其他地方的类似答案。
public static Date convertTimestampToDate(Timestamp timestamp) {
Instant ins=timestamp.toLocalDateTime().atZone(ZoneId.systemDefault()).toInstant();
return Date.from(ins);
}
java.sql.ResultSet rs;
//fill rs somehow
java.sql.Timestamp timestamp = rs.getTimestamp(1); //get first column
long milliseconds = timestamp.getTime() + (timestamp.getNanos() / 1000000);
java.util.Date date = return new java.util.Date(milliseconds);
时间戳是一个日期:https ://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
java.lang.Object
java.util.Date
java.sql.Timestamp