Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the remaining dates of current week using java

Tags:

java

date

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.

like image 609
Lily Avatar asked Dec 23 '22 17:12

Lily


2 Answers

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]
like image 107
Arvind Kumar Avinash Avatar answered Dec 27 '22 11:12

Arvind Kumar Avinash


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++));
}
like image 43
Joakim Danielson Avatar answered Dec 27 '22 10:12

Joakim Danielson