postgresql 如何使用 java 提取 Postgres 时间戳字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6182458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract a Postgres timestamp field using java?
提问by deltanovember
My data is in format:
我的数据格式如下:
2010-12-01 09:59:00.423
2010-12-01 09:59:00.423
getDate in Java only returns the date portion. Is there a way to also extract the time?
Java 中的 getDate 只返回日期部分。有没有办法也提取时间?
回答by BalusC
The SQL DATE
type indeed only contains the date portion, not the time. But your column is apparently of TIMESTAMP
type, so to get the full timestamp, use ResultSet#getTimestamp()
instead.
SQLDATE
类型确实只包含日期部分,而不包含时间。但是您的列显然是TIMESTAMP
类型的,因此要获得完整的时间戳,请ResultSet#getTimestamp()
改用。
Date date = resultSet.getTimestamp("columnname");
// ...
It returns java.sql.Timestamp
which is a subclass of java.util.Date
, so the upcast is safe.
它返回java.sql.Timestamp
which 是 的子类java.util.Date
,因此向上转换是安全的。