How to convert LocalDateTime
to java.sql.Date
in java-8?
My search on internet mostly give me Timestamp
related code or LocalDate
to java.sql.Date
. I'm looking for LocalDateTime
to java.sql.Date
.
You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.
A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30 . LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second.
Convert LocalDate To Date in JavaStep 1: First Create the LocalDate object using either now() or of() method. now() method gets the current date where as of() creates the LocalDate object with the given year, month and day. Step 2: Next, Get the timezone from operating system using ZoneId. systemDefault() method.
There is no direct correlation between LocalDateTime
and java.sql.Date
, since former is-a timestamp, and latter is-a Date.
There is, however, a relation between LocalDate
and java.sql.Date
, and conversion can be done like this:
LocalDate date = //your local date java.sql.Date sqlDate = java.sql.Date.valueOf(date)
Which for any given LocalDateTime
gives you the following code:
LocalDateTime dateTime = // your ldt java.sql.Date sqlDate = java.sql.Date.valueOf(dateTime.toLocalDate());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With