Get the weeknumber from a given date in Java FX

前端 未结 4 878
孤城傲影
孤城傲影 2020-12-11 04:46

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

4条回答
  •  無奈伤痛
    2020-12-11 05:23

    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());
    

    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.

提交回复
热议问题