50

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.

YvesHendseth
  • 1,149
  • 1
  • 10
  • 27

2 Answers2

91

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());
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 14
    Careful about Locale.getDefault(). In some places (e.g Canada) it can return a week that starts on a Sunday. Use "Locale.ISO" for weeks starting on a Monday. – Leo Ufimtsev Jan 24 '16 at 14:12
  • 18
    There is no Locale.ISO, but there is `WeekFields.ISO` – Michael Piefel Apr 10 '17 at 15:16
  • This is very important to notice. Probably you want to have `WeekFields.ISO`! – membersound Sep 11 '19 at 12:03
  • @membersound That depends on whether you want to use the ISO definition, or if you want the rule that is common for your locale. – Mark Rotteveel Sep 11 '19 at 12:20
  • Indeed I wasn't aware that eg in the US the first day might often be `Sunday`. Nonetheless, if you develop an application for european customers, but your systems defaults local is `en_US` (which is often the case for developers!), then your result will be different than expected if you didn't notice this. – membersound Sep 11 '19 at 12:25
  • One doubt here. How to get the week number in 2 letter. var dateRangeFrom = "20200101" ; var weekFields:WeekFields = WeekFields.of(Locale.getDefault()); This will provide the result as Int = 1 .But I want it as Int = 01. – abc_spark Apr 06 '20 at 11:15
  • @abc_spark Integers are never padded. That is a presentation concern that you should handle with a suitable formatter, eg see [How can I pad an integer with zeros on the left?](https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left) and other questions on stack overflow. – Mark Rotteveel Apr 06 '20 at 11:32
  • @Mark. Ya. That is correct. Thanks for that. – abc_spark Apr 06 '20 at 11:52
60

The answer of Mark Rotteveel is almost right and again an example which kind of confusion potential there is in the class WeekFields (similar sounding method names, but deviating from intuitive civil usage). The right code requires another field:

LocalDate date = LocalDate.now();
TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear(); 
int weekNumber = date.get(woy);

See also the similar debate on this SO-post, especially the discussion and comments about the answer of @kleopatra.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • 1
    I have updated my answer based on yours. I didn't have access to Java 8 here, so I had to guess (and it looks like I didn't read the [`WeekFields`](http://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html) class documentation to closely, as it does explain this difference. – Mark Rotteveel Sep 24 '14 at 10:40
  • @MarkRotteveel OK, you now have my upvote ;-) For me, the name "weekOfWeekBasedYear" is too technical and remains annoying. Even the ISO-8601-paper does not contain this term (it just uses terms like "calendar week" or "week date"). – Meno Hochschild Sep 24 '14 at 10:51
  • 3
    Careful about Locale.getDefault(). In some places (e.g Canada) it can return a week that starts on a Sunday. Use "Locale.ISO" for weeks starting on a Monday. – Leo Ufimtsev Jan 24 '16 at 14:12
  • 5
    There is no Locale.ISO, but there is `WeekFields.ISO` – Michael Piefel Apr 10 '17 at 15:16