Issue with parsing LocalDate to Date in Java [duplicate]

58
June 13, 2019, at 3:20 PM

This question already has an answer here:

  • Convert java.time.LocalDate into java.util.Date type 11 answers
  • Convert between LocalDate and sql.Date [duplicate] 1 answer

I am trying to parse LocalDate to date in my Java code but I keep getting the following error:

{code: "unknown.unexpected", detail: "Text '02/28/1936' could not be parsed at index 0", meta: null}

My code is as follows:

private Date dateOfBirth;
public SearchByDateCommand(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth != null ? Date.valueOf(dateOfBirth) : null;
}

What could I be doing wrong here?

Answer 1

Use this:

private Date dateOfBirth;
public SearchByDateCommand(LocalDate dateOfBirth) {
    this.dateOfBirth = dateOfBirth != null ? Date.from(dateOfBirth.atStartOfDay(ZoneId.systemDefault()).toInstant()) : null;
}

You will have to add a time to the LocalDate, interpret the date and time within a time zone, get the number of seconds / milliseconds since epoch, and lastly, create a java.util.Date.

READ ALSO
Nonblocking Java Async Processing - how to constrain memory usage?

Nonblocking Java Async Processing - how to constrain memory usage?

I'm coming back to Java after a few years away, and have been excited to see the introduction of non-blocking async support in the new javanet

48
Filter log by conversion pattern [%t]

Filter log by conversion pattern [%t]

I am using the log4j2 version 211

58
Different deserialization behavior between Java 8 and Java 11

Different deserialization behavior between Java 8 and Java 11

I have a problem with deserialization in Java 11 that results in a HashMap with a key that can't be foundI would appreciate if anyone with more knowledge about the issue could say if my proposed solution looks ok, or if there is something better I could do

37
UDP-Java Socket how can I exceed args.length > 2 to test how robust the client server connection is?

UDP-Java Socket how can I exceed args.length > 2 to test how robust the client server connection is?

I tried to enter a different IntetAdress object with googlede before it was initialized to null, which does not make a difference in output

50