-2

input list

  1. from date ex) 2020-10-01
  2. to date ex) 2020-10-30
  3. List [day of week] ex) [sun,mon....]
  4. List [week] ex) [1,4,5]

I would like to know how to get a specific day of the week between the two dates. Thank.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
sc m
  • 13
  • 2
  • 1
    Allow my honesty, this is not a very good question by Stack Overflow standards. Even when you don’t know where to begin, begin by searching and seeing if you can put an attempt together. If that fails, post a question and explain whet you found and what you tried and how it failed, and we immediately have a much better starting point for helping you progress. [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) And by all means don’t despair, it takes a bit to learn how to ask a good question, just keep on learning as you ask more questions. See you around. – Ole V.V. Sep 18 '20 at 15:22
  • 1
    The list of weeks, is that week of month, week of year, or …? And will the two dates always be in the same month or at least in the same year or at least at most a year apart? – Ole V.V. Sep 18 '20 at 15:25
  • Does this answer your question? [How to get all weekend between two specific day of month using java8 time API? \[closed\]](https://stackoverflow.com/questions/53491586/how-to-get-all-weekend-between-two-specific-day-of-month-using-java8-time-api) Can you substitute your own days of week instead of Sat and Sun in that question? – Ole V.V. Sep 18 '20 at 15:55

3 Answers3

1

from date ex) 2020-10-01 to date ex) 2020-10-30

Your input string is already in the ISO8601 format for date and therefore it can be parsed without providing a DateTimeFormatter explicitly. In order to get the output string in a custom format (e.g. yyyy-MM-dd), you need to format the date object using DateTimeFormatter.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String inputStrDate = "2020-10-01";
        LocalDate date = LocalDate.parse(inputStrDate);

        String outputStrDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(outputStrDate);
    }
}

Output:

2020-10-01

However, if your input is some other format, you will need to use DateTimeFormatter in order to parse it to a date object.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Formatter for input string
        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");

        String inputStrDate = "10-01-2020";
        LocalDate date = LocalDate.parse(inputStrDate, inputFormat);

        // Formatter for output string
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String outputStrDate = date.format(outputFormat);
        System.out.println(outputStrDate);
    }
}

Output:

2020-10-01

Learn more about date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1
for(LocalDate d = fromDate; !d.isAfter(toDate); d = d.plusDays(1)) { // 일정 시작 ~ 끝 loop
        for (Integer wf : weekOfMonth) {
            for (Integer df : dayOfWeek) {
                offDay = d.with(fieldWeek, wf)
                        .with(fieldDay, df);

                
                if (d.getMonth() == offDay.getMonth() && !offDays.contains(offDay)) {
                    offDays.add(offDay);
                }
            }
        }
    }

Sorry for asking the wrong question. And thank you very much.

I've already made it, but I've studied your code.

sc m
  • 13
  • 2
0

java.time

I too recommend that you use java.time, the modern Java date and time API, for your date work. My shot is:

    LocalDate fromDate = LocalDate.of(2020, Month.OCTOBER, 1);
    LocalDate toDate = LocalDate.of(2020, Month.OCTOBER, 30);
    List<DayOfWeek> daysOfWeek = List.of(DayOfWeek.SUNDAY, DayOfWeek.MONDAY);
    List<Integer> weeks = List.of(1, 4, 5);
    
    if (! YearMonth.from(fromDate).equals(YearMonth.from(toDate))) {
        throw new IllegalStateException("Covering more than one month is not yet supported");
    }
    
    WeekFields wf = WeekFields.SUNDAY_START;
    for (int week : weeks) {
        for (DayOfWeek dow : daysOfWeek) {
            LocalDate date = fromDate.with(wf.weekOfMonth(), week)
                    .with(wf.dayOfWeek(), dow.get(wf.dayOfWeek()));
            // Is date inside interval?
            if (! (date.isBefore(fromDate)  || date.isAfter(toDate))) {
                System.out.println(date);
            }
        }
    }

Output:

2020-10-18
2020-10-19
2020-10-25
2020-10-26

The dates printed are Sunday and Monday of weeks 4 and 5 of October defining weeks in the American way where the week begins on Sunday (since you mentioned Sunday first in your example list) and week 1 is the week of October 1. Sunday and Monday of week 1 are not printed because they fall before October 1, that is, in September.

Consider which week scheme you require. You may use for example WeekFields.ISO or WeekFields.of(Locale.getDefault()).

I am finding the week first, then the day of week, because to me this is the natural way. I need to use the WeekFields object for both adjustments to make sure that the chosen week scheme is respected.

If you need to cover more than one calendar month, iterate over the months and do the same for each. Also check that the result date falls within the month so duplicates near month borders are ignored.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161