从Java FX中获取给定日期的周数 [英] Get the weeknumber from a given date in Java FX
问题描述
我有一个javafx.scene.control.DatePicker。我想从所选日期中提取(区域设置)周数。到目前为止,我还没有找到解决方案,我不想编写自己的算法。我使用Java8并希望它可以在新的Java时间库中使用。
I have a javafx.scene.control.DatePicker. I want to extract the (Locale) week number from the selected date. Until now i haven't found a solution and i prefer not to write my own algorithm. I use Java8 and hope it is possible in the new java time library.
推荐答案
Java-8解决方案可以考虑在内使用的值来定义一周的本地定义日期选择器:
The Java-8-solution can take into account the local definition of a week using the value of a date-picker:
LocalDate date = datePicker.getValue(); // input from your date picker
Locale locale = Locale.US;
int weekOfYear = date.get(WeekFields.of(locale).weekOfWeekBasedYear());
还要记住,流行的替代Joda-Time不支持这样一个本地化的周 - 年字段。例如:在ISO-8601(广泛使用)中,本周从周一开始,在美国周日开始。此外,在给定日历年中第一周开始的项目取决于区域设置。
Also keep in mind that the popular alternative Joda-Time does not support such a localized week-of-year fields. For example: In ISO-8601 (widely used) the week starts with Monday, in US with Sunday. Also the item when the first week of year starts in a given calendar year is dependent on the locale.
这篇关于从Java FX中获取给定日期的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!