Ariel Grabijas
Ariel Grabijas

Reputation: 1512

Insert/select joda-time date types into/from database

What's the best way to insert jodatime objects into database using JDBC, and how to get them from it? I used to use MutableDateTime, and covert it into Timestamp before insert. But when I want to get my data from database, i got an issue.. i dont know how to do it, and how to parse types. So once again: how to insert and select Joda-Time objects into/from database.

Upvotes: 0

Views: 725

Answers (3)

leonbloy
leonbloy

Reputation: 75916

JodaTime objets are more expressive than the DateTime datatypes than SQL and most (if not all) databases support, hence there is no general foolproof recipe. Related question

Upvotes: 0

Jeff Miller
Jeff Miller

Reputation: 1434

You can use an ORM that supports custom types. For example, sormula Joda Time example. Implement a TypeTranslator like LocalDateTranslator and all uses of corresponding class, LocalDate, will use type translator to convert to/from column in db.

@ExplicitType(type=LocalDate.class, translator=LocalDateTranslator.class)
public class StudentLD
{
    int id;
    String firstName;
    String lastName;
    LocalDate graduationDate;
...

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691765

If you have a Timestamp, and want to convert it back to a MutableDateTime, you just need

new MutableDateTime(timestamp.getTime());

Upvotes: 1

Related Questions