0

In my project I need to select date and time when a product was created and to save it in a database. I'm making a desktop application using JavaFX and I have MySQL DB (project requirements, can't change).

My initial idea was to use javafx.scene.control.DatePicker for date & time input, and java.sql.Timestamp in the backend, but I'm unable to find a way to convert DatePicker value to Timestamp format. I need to preserve year, month, day, hours, minutes and seconds.

Is there any way to do this? If not, what other data types do you suggest?

Thanks.

vtomic85
  • 590
  • 1
  • 11
  • 32
  • How are you interfacing between JAvaFX and your database? JDBC or JPA? For JDBC all you need is `preparedStatement.set(index, Date.valueOf(datePicker.getValue()));`. – James_D Aug 03 '17 at 19:56
  • @James_D I have a class Product with Timestamp field, and ProductDAO where I'm inserting all the Product fields into DB. So basically I need to first convert DatePicker's value to Product.timeStamp. If that's not an option, I might as well pass it directly to ProductDAO, but that would be an uglier solution... Edit: I'm using JDBC – vtomic85 Aug 03 '17 at 20:00
  • 1
    A `Timestamp` encapsulates both a date and time, so a `java.sql.Date` might make more sense. But for a `Timestamp` you can do something like `Timestamp.valueOf(datePicker.getValue().atTime(0,0))`. – James_D Aug 03 '17 at 20:04

0 Answers0