Example
As per today's date 24 june 2019
(which is week 24 in a year) then it should show the below details.
data:[
week:24
{
startDate:24-06-2019,
endDate: 28-06-2019
},
week: 25
{
startDate: 01-07-2019,
endDate: 05-07-2019
}
Similar data for one year. How can I achieve this data in java. As I am new to java I am struggling to solve this issue. Please help with your explanation or code if possible.
PS: There are 53 weeks in a year.
I would perform this with a good old loop :
public void workingDayOfYear() {
LocalDate date = LocalDate.of(2019, 6, 24); // Java 8 date
int initialWeekOfyear = date.get(WeekFields.of(Locale.UK).weekOfWeekBasedYear());
int weekOfYear = initialWeekOfyear;
do {
LocalDate firstDayOfWeek = date.with(WeekFields.of(Locale.UK).dayOfWeek(), 1L);
LocalDate lastWorkingDayOfWeek = date.with(WeekFields.of(Locale.UK).dayOfWeek(), 5L);
StringBuilder result = new StringBuilder("Week : ");
result.append(weekOfYear);
result.append(", start=");
result.append(firstDayOfWeek);
result.append(", end=");
result.append(lastWorkingDayOfWeek);
System.out.println(result.toString());
date = date.plusWeeks(1);
} while ((weekOfYear = date.get(WeekFields.of(Locale.UK).weekOfWeekBasedYear())) != initialWeekOfyear);
}
Output:
Week : 26, start=2019-06-24, end=2019-06-28
Week : 27, start=2019-07-01, end=2019-07-05
...
Week : 24, start=2020-06-08, end=2020-06-12
Week : 25, start=2020-06-15, end=2020-06-19
Notice that the 53rd week you need is in fact the first week of 2020.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.