0

I have a table with a DateTime column. In java, I get localDatetime (now):

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); 

i want to format this to sql Datetime to insert into MySQL. Thank you!

Dũng Hoàng
  • 39
  • 1
  • 2
  • 6

1 Answers1

-2

You need to convert the LocalDateTime into milliseconds and create java.sql.date object from it, e.g.:

LocalDateTime now = LocalDateTime.now();
long millis = now.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli();
Date date = new Date(millis);
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102