Java LocalDate How to utilize

653 Views Asked by At

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.

3

There are 3 best solutions below

0
On BEST ANSWER

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.

0
On

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.

0
On
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.