Convert field timestamp data to a java.sql Date in jooq

5.3k views Asked by At

I retrieve timestamp type data from the mysql table. but I just need to return only the date part of this timestamp. Tried in convering timestamp to a date data type. But in jooq this gives some errors.Here is what I retrieve

 Field<Timestamp> transaction_date = LINKLK_TRANSACTIONS.MODIFIED_AT.as("transaction_date");
1

There are 1 answers

0
Lukas Eder On BEST ANSWER

This cannot work:

Field<Timestamp> transaction_date = LINKLK_TRANSACTIONS.MODIFIED_AT.as("transaction_date");

All you're doing is renaming your column to a different name that happens to contain the name "date". You have to use MySQL's date() function, for instance

Field<Date> transaction_date = DSL.date(LINKLK_TRANSACTIONS.MODIFIED_AT);

Or you can cast your Field:

Field<Date> transaction_date = LINKLK_TRANSACTIONS.MODIFIED_AT.cast(Date.class);

There are many other options to do the same, but the above will be sufficient for your particular use-case.