Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from Date picker

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 LocalDate to Date?

like image 237
Peter Penzov Avatar asked Dec 07 '13 20:12

Peter Penzov


People also ask

How do I get the Datepicker value?

If you want to get the date when the user selects it, you can do this: $("#datepicker"). datepicker({ onSelect: function() { var dateObject = $(this). datepicker('getDate'); } });

How do I get text from date picker?

final DatePicker datePicker = new DatePicker(LocalDate. now()); Date date = datePicker. getValue(); grid. add(datePicker, 1, 9);

How do you get flutter Datepicker value to the TextField?

To Implement Date Picker on TextField(): First of all, you need to add intl Flutter package in your dependency to get formatted date output from date picker. Add the following line in your pubspec. yaml file to add intl package to your project.


1 Answers

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
like image 99
cole.markham Avatar answered Oct 19 '22 06:10

cole.markham