skip
skip

Reputation: 12683

JPA: How to convert LocalDateTime to LocalDate

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

Upvotes: 14

Views: 38845

Answers (2)

土豆丝不辣
土豆丝不辣

Reputation: 229

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);
 }
}

Upvotes: 22

cghislai
cghislai

Reputation: 1801

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:

Upvotes: 2

Related Questions