views:

58

answers:

2

Hello
Are there any patterns or known ways of converting a date from a string representation to a numerical representation and vice versa?
Background:
I am using an Apache Derby database as the persistence for a Java program. I would like to do something like this:

Select * from MyTable where date_column > 20100914154503 order by date_column DESC
// 20100914154503 = 2010-09-14 15:45:03

My dates are stored in my java program I am using Joda Time ( http://joda-time.sourceforge.net/) DateTime object.
Thanks

+2  A: 

I'm not too sure what you're actually trying to do or why you need to use a string at all, but in general you should use a PreparedStatement. Then you can just set the date in the PreparedStatement using a java.sql.Timestamp:

DateTime date = ...;
Connection conn = ...;
PreparedStatement ps = conn.prepareStatement(
    "Select * from MyTable where date_column > ? order by date_column DESC");
ps.setTimestamp(1, new Timestamp(date.getMillis()));
ColinD
Timestamp storage in a database is nice and efficient, and you won't be gaining anything by storing a timestamp in a number field
Dunderklumpen
+1  A: 

Strongly consider looking into how your database manages dates and time, since it can probably do what you need. Remember that the date you have should be stored in a date field in the database table.

Thorbjørn Ravn Andersen