Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDateTime to java.sql.Date in java 8?

Tags:

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.

like image 846
Ajeetkumar Avatar asked Jul 14 '17 12:07

Ajeetkumar


People also ask

How do I get LocalDateTime in Java 8?

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.

What is the difference between date and LocalDateTime in Java?

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.

Can we convert LocalDate to date in Java?

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.


1 Answers

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()); 
like image 171
M. Prokhorov Avatar answered Sep 19 '22 21:09

M. Prokhorov