I want to get the value from JavaFX datepicker and store the value as Date Object:
final DatePicker datePicker = new DatePicker(LocalDate.now()); Date date = datePicker.getValue(); grid.add(datePicker, 1, 9);
Can you tell me how I can convert
As the name implies, LocalDate does not store a timezone nor the time of day. Therefore to convert to an absolute time you must specify the timezone and time of day. There is a simple method to do both, atStartOfDay(ZoneId). Here I use the default time zone which is the local timezone of the computer. This gives you an Instant object which represents and instant in time. Finally, Java 8 added a factory method to Date to construct from an Instant.
LocalDate localDate = datePicker.getValue(); Instant instant = Instant.from(localDate.atStartOfDay(ZoneId.systemDefault())); Date date = Date.from(instant); System.out.println(localDate + "\n" + instant + "\n" + date);
This will give you an output like below. Note that the Instant by default prints in UTC.
2014-02-25 2014-02-25T06:00:00Z Tue Feb 25 00:00:00 CST 2014
Of course, you will need to convert your java.util.Date into a java.time.LocalDate to set the value on the DatePicker. For that you will need something like this:
Date date = new Date(); Instant instant = date.toInstant(); LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(date + "\n" + instant + "\n" + localDate);
Which will give you output like:
Tue Feb 25 08:47:04 CST 2014 2014-02-25T14:47:04.395Z 2014-02-25