Get Week Number of LocalDate (Java 8)

YvesHendseth picture YvesHendseth · Sep 24, 2014 · Viewed 45.6k times · Source

I'm trying to get the Week Number of a full LocalDate with the format:

dd.MM.yyy

I haven't found a function in the Java 8 Date API wich returns the Week Number and i have tried to create a algorithm, but it did'nt work.

Answer

Mark Rotteveel picture Mark Rotteveel · Sep 24, 2014

One small warning. I haven't tested this yet, but looking at the API documentation of WeekFields and LocalDate.get, you should do something like:

LocalDate date = ...;
// Or use a specific locale, or configure your own rules
WeekFields weekFields = WeekFields.of(Locale.getDefault()); 
int weekNumber = date.get(weekFields.weekOfWeekBasedYear());