1

Im trying display all Mondays between two dates but I have no idea how to do it. I want ask user to input two dates using Scanner, to output all Mondays.

    String s = "2020-07-20";
    String e = "2020-09-20";
    LocalDate start = LocalDate.parse(s);
    LocalDate end = LocalDate.parse(e);
    List<LocalDate> totalDates = new ArrayList<>();

    while (!start.isAfter(end)) {
        totalDates.add(start);
        start = start.plusDays(1);

    }
marzocchi
  • 75
  • 1
  • 8
  • 1
    Does [this answer by Basil Bourque](https://stackoverflow.com/a/42100826/5772882) answer your question? That question is about Joda-Time, but that answer uses java.time just as you do. – Ole V.V. Jul 11 '20 at 14:12

4 Answers4

3

To get the the first Monday, use:

LocalDate monday = start.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));

Now you have to deal with an edge case: What if there is no Monday between start and end? That would mean that the Monday computed here is after end:

if (monday.isAfter(end)) {
    totalDates = List.of();
}

After that, you can get a sequence of Mondays with the convenient datesUntil method:

totalDates = monday.datesUntil(end, Period.ofWeeks(1)).collect(Collectors.toList());

Note that datesUntil does not include the end date. If you need the end date included, pass in end.plusDays(1).

Joni
  • 108,737
  • 14
  • 143
  • 193
2

You need to use getDayOfWeek(), here is the solution:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class AllMondays {
    public static void main(String[] args) {
        String s = "2020-07-20";
        String e = "2020-09-20";
        LocalDate start = LocalDate.parse(s);
        LocalDate end = LocalDate.parse(e);
        List<LocalDate> totalDates = new ArrayList<>();

        LocalDate nextMonday = start;
        int daysToAdvance = 1;
        while (!nextMonday.isAfter(end)) {
            if (nextMonday.getDayOfWeek() == DayOfWeek.MONDAY) {
                daysToAdvance = 7;
                totalDates.add(nextMonday);
            }
            nextMonday = nextMonday.plusDays(daysToAdvance);
        }
        System.out.println(totalDates);
    }
}
1

You can use .getDayOfWeek() to get DayOfWeek and compare with DayOfWeek.MONDAY

while (!start.isAfter(end)) {
    if(start.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
        totalDates.add(start);
    }
    start = start.plusDays(1);
}

And optimization is use start.plusWeeks(1) instead of start.plusDays(1) and you need to get the next monday before.

start = start.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
while (!start.isAfter(end)) {
    if(start.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
        totalDates.add(start);
    }
    start = start.plusWeeks(1);
}
Eklavya
  • 17,618
  • 4
  • 28
  • 57
0

Here "find_day" is the day you want to find in date range

SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            Date sub_start_date = format1.parse("01-06-2021");
            Date enddate = format1.parse("30-06-2021");
    
            Calendar min_date_c = Calendar.getInstance();
            min_date_c.setTime(sub_start_date);
            Calendar max_date_c = Calendar.getInstance();
            max_date_c.setTime(enddate);
    
            for (Calendar loopdate = min_date_c; min_date_c.before(max_date_c); min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
                int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
                // if (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.SATURDAY) {
                if (dayOfWeek == find_day) {
                    Calendar[] disabledDays = new Calendar[1];
                    disabledDays[0] = loopdate;
                    Date newdate = loopdate.getTime();
                    String strDate = format1.format(newdate);
                    Log.e("DATES>>> ", "" + strDate);
                }
            }
  • The question didn’t seem to be about Android, so suggesting not using java.time, the modern Java date and time API used in the question, and going back to the notoriously troublesome and long outdated `SimpleDateFormat` class and friends seems inappropriate here. – Ole V.V. Jun 17 '21 at 14:49
  • Ok, @OleV.V. thanks for your kind information. – Nitz_with_u Jun 18 '21 at 04:12