0

How to determine the next day's date by passing a certain day of the week?

For example, if today (03 June) I pass Monday, it will give me 07 JUNE because the next Monday will be 07 June?

We are on: Sunday 06 June, If I enter:

  • Sunday , return 06 June
  • Monday , return 07 June
  • Tuesday, return 08 June
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Use the `java.time` API and then `plus` one day. For example `ZonedDateTime.now().plusDays(1)`. – Zabuzard Jun 03 '21 at 15:11
  • 2
    I think this will help: https://stackoverflow.com/a/24177555/1039920 – Mark Sholund Jun 03 '21 at 15:24
  • @MarkSholund Spot on! (if I understood the question correctly) – Ole V.V. Jun 03 '21 at 15:28
  • 1
    See [`TemporalAdjusters.next( DayOfWeek )`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/temporal/TemporalAdjusters.html#next(java.time.DayOfWeek)). If you were to carefully edit your Question to be clear and specific, perhaps it could be reopened and a full Answer written. – Basil Bourque Jun 03 '21 at 15:29
  • If today was already a Monday, would you want today’s date or the following Monday? – Ole V.V. Jun 04 '21 at 04:37
  • As mentionned in the example; we are on Wednesday, If I enter the word Thursday, it will give me the next Thursday's Date. – Anis Saidani Jun 06 '21 at 11:33
  • @OleV.V. It will give today's date – Anis Saidani Jun 06 '21 at 11:34

1 Answers1

4

java.time

Use LocalDate#with(TemporalAdjuster) to get the required LocalDate.

Demo:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM", Locale.ENGLISH);
        System.out.println(getNextOrSameDate(DayOfWeek.SUNDAY).format(dtf));
        System.out.println(getNextOrSameDate(DayOfWeek.MONDAY).format(dtf));
        System.out.println(getNextOrSameDate(DayOfWeek.TUESDAY).format(dtf));
    }

    static LocalDate getNextOrSameDate(DayOfWeek dw) {
        return LocalDate.now().with(TemporalAdjusters.nextOrSame(dw));
    }
}

Output:

06 June
07 June
08 June

ONLINE DEMO

Alternatively, you can return the weekday name directly from the function.

Demo:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getNextOrSameWeekDayName(DayOfWeek.SUNDAY));
        System.out.println(getNextOrSameWeekDayName(DayOfWeek.MONDAY));
        System.out.println(getNextOrSameWeekDayName(DayOfWeek.TUESDAY));
    }

    static String getNextOrSameWeekDayName(DayOfWeek dw) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM", Locale.ENGLISH);
        return LocalDate.now().with(TemporalAdjusters.nextOrSame(dw)).format(dtf);
    }
}

ONLINE DEMO

Alternatively, you can pass the weekday name as String and return any of the things as shown above. However, this is error-prone and I recommend you pass an enum instead of a String as shown in previous examples.

Demo:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getNextOrSameWeekDayName("Sunday"));
        System.out.println(getNextOrSameWeekDayName("Monday"));
        System.out.println(getNextOrSameWeekDayName("Tuesday"));
    }

    static String getNextOrSameWeekDayName(String dw) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM", Locale.ENGLISH);
        return LocalDate.now()
                .with(TemporalAdjusters.nextOrSame(DayOfWeek.valueOf(dw.toUpperCase())))
                .format(dtf);
    }
}

ONLINE DEMO

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110