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 对象?
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
