How could I use @NamedQuery to get a coloumn as LocalDate type which is defined LocalDateTime type. How can i do it?
Asked
Active
Viewed 2.5k times
8
-
1You retrieve the field, and convert it in Java! Which is nothing to do with JPA – Aug 04 '18 at 08:35
2 Answers
14
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);
}
}

Vineeth Sai
- 3,389
- 7
- 23
- 34

土豆丝不辣
- 149
- 1
- 5
-
3Code-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 – Nick Dec 21 '18 at 07:22
-
The question was how he can convert LocalDateTime to LocalDate inside the query. – LosmiNCL Oct 22 '21 at 17:42
2
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.

cghislai
- 1,751
- 15
- 29