I'm trying to retrieve a Timestamp
from my DB and I need to convert it to a java.util.Date
I've been following several examples I've found in stackoverflow but I keep getting the following exception:
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
I don't know why, I've tried several ways of doing this, can you help me?
Here's my code:
//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();
...
}
Stop converting the values to strings and back. A java.sql.Timestamp
already is a java.util.Date
:
long now = System.currentTimeMillis();
while (rs.next()) {
Timestamp ts = rs.getTimestamp("HIGH_VALUE");
long diff = ts.getTime() - now;
... use diff ...
}
You should only be dealing with strings when you fundamentally want to do so. In this case, "finding the difference between a stored timestamp and the current time" has nothing to do with a string representation.
See more on this question at Stackoverflow