Java 从日期选择器获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20446026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value from Date picker
提问by Peter Penzov
I want to get the value from JavaFX datepicker and store the value as Date Object:
我想从 JavaFX datepicker 获取值并将值存储为日期对象:
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
?
你能告诉我如何转换LocalDate
为Date
吗?
采纳答案by cole.markham
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 不存储时区或一天中的时间。因此,要转换为绝对时间,您必须指定时区和时间。有一个简单的方法可以做到这两者,atStartOfDay(ZoneId)。这里我使用默认时区,即计算机的本地时区。这为您提供了一个 Instant 对象,它代表和即时。最后,Java 8 为 Date 添加了一个工厂方法以从 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.
这将为您提供如下所示的输出。请注意,默认情况下 Instant 以 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:
当然,您需要将 java.util.Date 转换为 java.time.LocalDate 以设置 DatePicker 上的值。为此,您将需要这样的东西:
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
回答by LeonidasCZ
I don't have IDE at hand but this should do the trick.
我手头没有 IDE,但这应该可以解决问题。
Date date = new Date(datePicker.getvalue().toEpochDay());
Or alternatively.
或者换一种方式。
LocalDate ld = datePicker.getValue();
Calendar c = Calendar.getInstance();
c.set(ld.getYear(), ld.getMonthValue(), ld.getDayOfMonth());
Date date = c.getTime();
回答by Dmitry Nelepov
Or you can use more simplest way
或者你可以使用更简单的方法
java.sql.Date gettedDatePickerDate = java.sql.Date.valueOf(myDatePicker.getValue());
And then you can perform operation within this date object.
然后你可以在这个日期对象中执行操作。
Convert to java.util.Date
转换成 java.util.Date
回答by mirkorossi95
LeonidasCZ answer is not 100% correct: java.util.Date
months range is from 0 to 11, but Calendar.getInstance()
is from 1 to 12, so you can do this:
LeonidasCZ 的答案不是 100% 正确:java.util.Date
月份范围是从 0 到 11,但从Calendar.getInstance()
1 到 12,所以你可以这样做:
LocalDate ld = datePicker.getValue();
Calendar c = Calendar.getInstance();
c.set(ld.getYear(), ld.getMonthValue() - 1, ld.getDayOfMonth());
Date date = c.getTime();
If you want to add hours, mins, seconds too:
如果您还想添加小时、分钟、秒:
LocalDate ld = datePicker.getValue();
Calendar c = Calendar.getInstance();
c.set(ld.getYear(), ld.getMonthValue() - 1, ld.getDayOfMonth(), hours, minutes, seconds);
Date date = c.getTime();