Soutenez Time.now : Rejoignez notre Plan Premium pour une expérience sans publicité ! Soutenez-nous : Passez Premium sans pub !

Week numbers in Java

To get the current ISO 8601 week number in Java, use the following code snippet:

import java.time.LocalDate; import java.time.temporal.IsoFields; int week = LocalDate.now().get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);

* Ensure your system or application regional settings are set to follow ISO 8601 standards (weeks start on Monday) if the output varies.

Instructions étape par étape

  1. Import java.time.LocalDate and java.time.temporal.IsoFields.
  2. Call LocalDate.now().get(IsoFields.WEEK_OF_WEEK_BASED_YEAR).
  3. For the matching ISO year use IsoFields.WEEK_BASED_YEAR.

Bon à savoir

Avoid the legacy java.util.Calendar week fields: they depend on the default Locale and can silently use Sunday-start US numbering. The java.time IsoFields API is locale-independent and always ISO 8601.