How could I use @NamedQuery to get a coloumn as LocalDate type which is defined LocalDateTime type. How can i do it?
JPA: How to convert LocalDateTime to LocalDate
Asked Answered
You retrieve the field, and convert it in Java! Which is nothing to do with JPA –
Hibernate
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTimeFromDateAndTime = LocalDateTime.of(date, time);
System.out.println(dateTimeFromDateAndTime);
LocalDate dateFromDateTime = LocalDateTime.now().toLocalDate();
LocalTime timeFromDateTime = LocalDateTime.now().toLocalTime();
System.out.println(dateFromDateTime);
System.out.println(timeFromDateTime);
}
}
Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. Thanks –
Owl
The question was how he can convert LocalDateTime to LocalDate inside the query. –
Thema
I assume that by a column which is defined LocalDateTime type
you have declared an entity with a LocalDateTime field.
If you want to make the date conversion on the database, i think you will have to use a native query. If you don't mind making the conversion in java, I see two options:
- Adding a converter on your entity field so that it can be a LocalDate binding a timestamp (https://docs.oracle.com/javaee/7/api/javax/persistence/Convert.html)
- Adding a @Transient getter so that you can make the conversion in the entity class.
© 2022 - 2024 — McMap. All rights reserved.