Reputation: 12662
With java.time.ZonedDateTime
, you can get the calendar week( say week 1).
import java.time.ZonedDateTime;
int getCalendarWeek(ZonedDateTime zonedDateTime){
int week = zonedDateTime.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
return week;
}
With year and a calendar week given (ex : 2019 and week 20 ) , how can you return a java.time.ZonedDateTime
?
The first day ( Monday) of the calendar week is the date. Whatever hour.
ZonedDateTime getZonedDateTime(int year, int weekNumber){
// ???
}
Upvotes: 0
Views: 295
Reputation: 729
You could use the with
operation in the ZonedDateTime to obtain a date based on a specific week. The WeekFields
class allows access to the TemporalFields
required to do this. See the sample below:
int week = 1;
int year = 2016;
WeekFields weekFields = WeekFields.of(Locale.getDefault());
ZonedDateTime ldt = ZonedDateTime.now()
.withYear(year)
.with(weekFields.weekOfYear(), week)
.with(weekFields.dayOfWeek(), 1);
Note that this does not solve the issue on the timezone, as it asumes the system default timezone.
Upvotes: 3