Reputation: 707
I am using the following code to get the current date and the next dates.
List<String> list=new ArrayList();
int nextWeekId=2;
int currentWeekId=1;
LocalDate localDate = LocalDate.now();
for (int i = 0; i < list.size(); i++) {
if (i >= 7) {
saveDetails(nextWeekId,list.get(i), java.sql.Date.valueOf(localDate.plusDays(i)));
} else {
saveDetails(currentWeekId, list.get(i), java.sql.Date.valueOf(localDate.plusDays(i)));
}
}
My list
size will always the size = 14
. The week should always start on Monday
. I want that if for example today is Friday
and date is 2020-07-10
. Then my system will store the date 2020-07-10 , 2020-07-11 , 2020-07-12
against currentWeekId
and the complete next seven days against nextWeekId
.
The order of accessing the values doesn't matter.
Upvotes: 4
Views: 813
Reputation: 371
You can use getWeek
of from the code given below, for current week you need to pass 1, next week 2, next to next week 3 and so on...
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class WeekDates {
public static void main(String[] args) {
System.out.println(getWeek(1));
System.out.println(getWeek(2));
System.out.println(getWeek(3));
}
public static List<LocalDate> getWeek(int week) {
List<LocalDate> list = new ArrayList<>();
LocalDate date = LocalDate.now();
int days = 7 - date.getDayOfWeek().getValue() + 1;
if (week != 1) {
date = date.plusDays((week - 2) * 7 + days);
days = 7;
}
for (int i = 0; i < days; i++) {
list.add(date);
date = date.plusDays(1);
}
return list;
}
}
Upvotes: 1
Reputation: 18430
You try this way
LocalDate localDate = LocalDate.now();
// Current week dates
LocalDate nextWeekStart = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
List<LocalDate> currentWeekdays = localDate.datesUntil(nextWeekStart).collect(Collectors.toList());
// Next week dates
LocalDate nextWeekStart2 = nextWeekStart.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
List<LocalDate> nextWeekdays = nextWeekStart.datesUntil(nextWeekStart2).collect(Collectors.toList());
Upvotes: 1
Reputation: 51945
Check what day of week the start date is and then loop from that day to Sunday
LocalDate date = LocalDate.of(2020, 7, 10);
List<LocalDate> thisWeek = new ArrayList<>();
int dayCount = 0;
for (int i = date.getDayOfWeek().getValue(); i <= DayOfWeek.SUNDAY.getValue(); i++) {
thisWeek.add(date.plusDays(dayCount++));
}
To use this solution for saving the dates as in the question it would look something like this
int dayCount = 0;
for (int i = date.getDayOfWeek().getValue(); i <= DayOfWeek.SUNDAY.getValue(); i++) {
saveDetails(currentWeekId,list.get(dayCount), date.plusDays(dayCount++));
}
for (DayOfWeek day : DayOfWeek.values()) {
saveDetails(nextWeekId,list.get(dayCount), date.plusDays(dayCount++));
}
Upvotes: 2
Reputation: 400
//curent day
LocalDate localDate = LocalDate.now();
// find name of the day
String dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT_STANDALONE , Locale.US);
// set a week distance from current day
LocalDate deadlineDate = LocalDate.now().plusDays(7);
// add dates to a list from current day to a week distance
List<LocalDate> dates = Stream.iterate(localDate, date -> date.plusDays(7))
.limit(ChronoUnit.DAYS.between(localDate, deadlineDate))
.collect(Collectors.toList());
System.out.println(dates.size() + " days to finish the order starting from " + dayOfWeek); // 7 days to finish the order starting from Sat
System.out.println(dates); // [2020-07-11, 2020-07-18, 2020-07-25, 2020-08-01, 2020-08-08, 2020-08-15, 2020-08-22]
Upvotes: 1
Reputation: 40034
Here is one way.
LocalDate localDate = LocalDate.now();
List<LocalDate> dates = localDate
.datesUntil(localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
.collect(Collectors.toList());
System.out.println(dates);
Prints
[2020-07-11, 2020-07-12]
Upvotes: 1
Reputation: 79085
In order to get the remaining days of the current week, you need to iterate from the current date until the next Monday as shown below:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
LocalDate firstDayOfNextWeek = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
while (localDate.isBefore(firstDayOfNextWeek)) {
System.out.println(localDate);
localDate = localDate.plusDays(1);
}
}
}
Output:
2020-07-11
2020-07-12
Note that LocalDate#with
returns a copy of the target object with one element changed; this is the immutable equivalent to a set
method on a JavaBean.
[Update]: Given below is another way (Cortesy: Basil Bourque) to do it:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
LocalDate firstDayOfNextWeek = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
List<LocalDate> remainingDays = localDate.datesUntil(firstDayOfNextWeek)
.collect(Collectors.toList());
System.out.println(remainingDays);
}
}
Output:
[2020-07-11, 2020-07-12]
Check LocalDate#datesUntil
to learn more about it.
[Another update]
Posting this update to help OP list all days of the next week:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
// Remaining days of the current week
LocalDate firstDayOfNextWeek = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
List<LocalDate> remainingDays = localDate.datesUntil(firstDayOfNextWeek).collect(Collectors.toList());
System.out.println(remainingDays);
// Days of next week
LocalDate firstDayOfNextToNextWeek = firstDayOfNextWeek.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
List<LocalDate> daysOfNextWeek = firstDayOfNextWeek.datesUntil(firstDayOfNextToNextWeek)
.collect(Collectors.toList());
System.out.println(daysOfNextWeek);
}
}
Output:
[2020-07-11, 2020-07-12]
[2020-07-13, 2020-07-14, 2020-07-15, 2020-07-16, 2020-07-17, 2020-07-18, 2020-07-19]
Upvotes: 3