Get the weeknumber from a given date in Java FX

前端 未结 4 886
孤城傲影
孤城傲影 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:09

    You can use http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#get-java.time.temporal.TemporalField-

    LocalDate localDate = LocalDate.of(2014, 9, 18); // assuming we picked 18 September 2014
    int weekNumber = localDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
    

    This will give you the week number based on ISO convention.

    For a locale based evaluation :

    LocalDate localDate = LocalDate.of(2014, 9, 18); // assuming we picked 18 September 2014
    WeekFields weekFields = WeekFields.of(Locale.US);
    int weekNumber = localDate.get(weekFields.weekOfWeekBasedYear());
    

提交回复
热议问题