JPA: How to convert LocalDateTime to LocalDate
Asked Answered
P

2

10

How could I use @NamedQuery to get a coloumn as LocalDate type which is defined LocalDateTime type. How can i do it?

Parathyroid answered 4/8, 2018 at 7:8 Comment(1)
You retrieve the field, and convert it in Java! Which is nothing to do with JPAHibernate
B
20
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);
 }
}
Burcham answered 21/12, 2018 at 7:0 Comment(2)
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. ThanksOwl
The question was how he can convert LocalDateTime to LocalDate inside the query.Thema
S
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:

Saleratus answered 4/8, 2018 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.