2

How can I get a date by day's name?

For example:

Input: Monday
Output: 02/08/2021

Input: Tuesday
Output: 03/08/2021

I want to get the closest date of the day.

Ziv Sion
  • 424
  • 4
  • 14
  • 3
    What did your search bring up? I hope you will find pages advising the use of `DayOfWeek` and `LocalDate`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 01 '21 at 06:48
  • 1
    By "closest" do you mean the closest from today? – Sweeper Aug 01 '21 at 07:00
  • Closest non-past day (up to 6 days into the future) or closest regardless of future or past (at most 3 days away from today)? – Ole V.V. Aug 01 '21 at 07:51

2 Answers2

1

Assuming that you want to find the closest day from today that has a specific day of week, one way to do this is to compute both the next and previous day from today that has that day of week, and compare them:

private static LocalDate closestDOW(DayOfWeek dow) {
  LocalDate today = LocalDate.now();
  LocalDate next = today.with(TemporalAdjusters.nextOrSame(dow));
  LocalDate previous = today.with(TemporalAdjusters.previousOrSame(dow));
  if (ChronoUnit.DAYS.between(today, next) < ChronoUnit.DAYS.between(previous, today)) {
    return next;
  } else {
    return previous;
  }
}

Alternatively, work out whether the next such day is at most three days away. If it is, then it is closer than the previous such day.

private static LocalDate closestDOW(DayOfWeek dow) {
  LocalDate today = LocalDate.now();
  int daysDiff = today.getDayOfWeek().getValue() - dow.getValue();
  int daysUntilNextDOW = daysDiff >= 0 ? 7 - daysDiff : -daysDiff;
  if (daysUntilNextDOW <= 3) {
    return today.plusDays(daysUntilNextDOW);
  } else {
    return today.with(TemporalAdjusters.previousOrSame(dow));
  }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

This is my understanding of what the OP wants -

Given a day of the week as input, print the date (having the same day of the week as the input) which is closest to today.

We can do this using LocalDate, DayOfWeek and TemporalAdjuster.

The logic is -

  • Convert the input day of week to an instance of DayOfWeek.
  • If today is the same day of week as the input, print today's date and stop, else proceed to the next steps.
  • Get the date of the same day of the week in the previous week.
  • Get the date of the same day of the week in the next week.
  • Check which day is closer to today by using .toEpochDay().
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public static void main(String[] args) {
    String inputDayOfWeekString = "SUNDAY";
    DayOfWeek inputDayOfWeek = DayOfWeek.valueOf(inputDayOfWeekString);
    
    LocalDate today = LocalDate.now();
    if (today.getDayOfWeek().equals(inputDayOfWeek)) {
        System.out.println(today);
    } else {
        LocalDate sameDayNextWeek = today.with(TemporalAdjusters.next(inputDayOfWeek));
        LocalDate sameDayPreviousWeek = today.with(TemporalAdjusters.previous(inputDayOfWeek));
        LocalDate dateCloserToToday = (sameDayNextWeek.toEpochDay() - today.toEpochDay()) < (today.toEpochDay() - sameDayPreviousWeek.toEpochDay()) ? sameDayNextWeek : sameDayPreviousWeek;
        System.out.println(dateCloserToToday);
    }
}
Dhruv Saraswat
  • 858
  • 9
  • 13