I want to select date in particular format in multiselect of jpa criteria query
like we use
select to_char(tn.dbdate,'yyyy-MM-dd') from transaction tn
in oracle.
I am able to use
query.multiselect(cb.function("TO_CHAR",String.class,transaction.get("dbdate")) );
but this returns date in database format i.e.
Wed Apr 2 12:20:50 2014
but how to get this in specific date format
'yyyy-MM-dd'
In current query no format is given as an argument to TO_CHAR function. That's why it cannot do much else than fall back to default. As documented, more than one arguments can also be passed to database function via CriteriaBuilder.function:
query.multiselect(
cb.function("TO_CHAR",
String.class,transaction.get("dbdate"),
cb.literal("yyyy-MM-dd")));
You can take the date from DB and format it as you want later.
This worked for me:
Expression<String> dateStringExpr = cb.function("to_char", String.class,
root.get("effectiveStartDate"), cb.literal("YYYY-MM-DD HH12:MI:SS"));
ls.add(cb.like(cb.lower(dateStringExpr), "%" + effectiveStartDate.get().toLowerCase() + "%"));