tl;dr
LocalDate localDate =
YearWeek // Represent an entire week of a week-based year per the ISO 8601 standard definition of a week.
.of( // Instantiate a `YearWeek` object.
2019 , // Specify the week-based year number, NOT the calendar year.
51 // Specify the week number, 1-52 or 1-53.
)
.atDay(
DayOfWeek.of( 1 ) // The value 1 yields a `DayOfWeek.MONDAY` object.
)
;
org.threeten.extra.YearWeek
The Answer by Sweeper looks correct. But there is a more specialized class for this.
If doing much work with weeks of week-based years per the ISO 8601 definition of week, use the YearWeek
class found in the ThreeTen-Extra library. This library adds extra functionality to the java.time classes built into Java 8 and later.
Determine the week.
YearWeek yearWeek = YearWeek.of( 2019 , 51 ) ;
Get a LocalDate
for the day-of-week within that week.
LocalDate localDate = yearWeek.atDay( DayOfWeek.MONDAY ) ;
For the day-of-week, you should be using DayOfWeek
enum objects in your code rather than mere integer numbers. To get a DayOfWeek
from an original number 1-7 for Monday-Sunday, call DayOfWeek.of( x )
.
DayOfWeek dow = DayOfWeek.of( 1 ) ; // 1 = Monday, 7 = Sunday.
Putting that all together we get this one-liner.
LocalDate localDate = YearWeek.of( 2019 , 51 ).atDay( DayOfWeek.of( 1 ) ) ;
To be clear… The ISO 8601 definition of a week is:
- Week # 1 contains the first Thursday of the year.
- Weeks start on a Monday, ending on a Sunday.
- A year has either 52 or 53 complete 7-day weeks.
- The first/last weeks of the week-based year may contain the trailing/leading days of the previous/next calendar years. Thus, the calendar year of those days differ from the week-based year.