0

So I have a field of type Date, is there a way I can convert this into LocalDateTime in its getter method? What I have so far is..

private Date date;

public getDate(){
return date == null ? null : LocalDateTime.of(date) }

But it doesnt seem to work.

sujith
  • 15
  • 5
  • 2
    Does this answer your question? [Converting between java.time.LocalDateTime and java.util.Date](https://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date) – Francesco May 06 '20 at 07:45
  • 1
    Creating something that follows Java syntax would be a great start. You didn't define return type of the method, missing semicolon on the end... – Amongalen May 06 '20 at 07:48
  • And you probably should define what "it doesnt seem to work" means in your case other than non-compiling code. – Amongalen May 06 '20 at 07:49
  • `Date` represents an instant. To convert it to a `LocalDateTime` you need to specify a timezone. – Sweeper May 06 '20 at 07:55

1 Answers1

2

You can do something like below

LocalDateTime getLocalDateTime(Date date){
        return date == null ? null : date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
QuickSilver
  • 3,915
  • 2
  • 13
  • 29