How can I convert a Timestamp into either Date or DateTime object?(如何将 Timestamp 转换为 Date 或 DateTime 对象?)
问题描述
我正在使用 ResultSet.getTimestamp()
从数据库中检索时间戳对象,但我想要一种简单的方法来获取 MM/DD/格式的日期YYYY
和 HH:MM xx
格式的时间.我正在修补,看起来我可以通过使用 Java 中的 Date 和/或 DateTime 对象来做到这一点.这是最好的方法,还是我什至需要转换时间戳来完成这个?任何建议都会有所帮助.
I'm retrieving a timestamp object from a database using ResultSet.getTimestamp()
, but I'd like an easy way to get the date in the format of MM/DD/YYYY
and the time in a format of HH:MM xx
. I was tinkering around, it it looks as though I can do such by making use of the Date and/or DateTime objects within Java. Is that the best way to go, or do I even need to convert the timestamp to accomplish this? Any recommendations would be helpful.
....
while(resultSet.next()) {
Timestamp dtStart = resultSet.getTimestamp("dtStart");
Timestamp dtEnd = resultSet.getTimestamp("dtEnd");
// I would like to then have the date and time
// converted into the formats mentioned...
....
}
....
推荐答案
java.sql.Timestamp
是 java.util.Date
.所以,只是向上吧.
java.sql.Timestamp
is a subclass of java.util.Date
. So, just upcast it.
Date dtStart = resultSet.getTimestamp("dtStart");
Date dtEnd = resultSet.getTimestamp("dtEnd");
使用 SimpleDateFormat
并创建 Joda DateTime
从现在开始应该是直截了当的.
Using SimpleDateFormat
and creating Joda DateTime
should be straightforward from this point on.
这篇关于如何将 Timestamp 转换为 Date 或 DateTime 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Timestamp 转换为 Date 或 DateTime 对象?


基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01