我尝试从我的数据库中检索Timestamp
,我需要将其转换为java.util.Date
我一直在关注我在stackoverflow中找到的几个例子,但我一直遇到以下异常:
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
我不知道为什么,我已经尝试了几种方法,你能帮助我吗?
这是我的代码:
//select all partitions from table "LDRS"
stmtSelect = connection.prepareCall("Select HIGH_VALUE from user_tab_partitions where table_name = 'LDRS'");
ResultSet rs = stmtSelect.executeQuery();
SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
while (rs.next()) {
try {
Timestamp ts = rs.getTimestamp("HIGH_VALUE");
Date date1 = myFormat.parse(ts.toString());
Date date2 = myFormat.parse(""+new Date());
// compares the current date with the partition date, if the difference between them is more than 30 days
// drops the partition
long diff = date2.getTime() - date1.getTime();
...
}
答案 0 :(得分:1)
停止将值转换为字符串并返回。 java.sql.Timestamp
已经 一个java.util.Date
:
long now = System.currentTimeMillis();
while (rs.next()) {
Timestamp ts = rs.getTimestamp("HIGH_VALUE");
long diff = ts.getTime() - now;
... use diff ...
}
当您从根本上想要这样做时,只应处理字符串。在这种情况下,"找到存储的时间戳和当前时间之间的差异"与字符串表示无关。