-4

I have a requirement which is as follows: for example if today is Sunday then I need to get first Sunday and last Sunday in next month, and so on for all days in the week. I need to get the first and last day in next month.

Any idea on how I can do it

Is there a generic way to do it, or do I need to deal with each day separately? I have tried many ways, but all I found is each day has a special formula.

The technology I am using is Java.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Maybe duplicated: https://stackoverflow.com/questions/57625854/localdatetime-java-get-same-day-of-week-for-next-month – Elyas Hadizadeh May 03 '20 at 07:16
  • It’s related and a helpful link, @ElyasHadizadeh, thank you. I don’t consider it an exact duplicate. – Ole V.V. May 03 '20 at 07:32
  • Welcome to Stack Overflow. Your question has got a couple of downvotes, that’s usual for new users here. It takes a bit to learn [how to ask a good question](https://stackoverflow.com/help/how-to-ask). And we’re picky. I didn’t downvote. I think the downvotes in this case are probably because you haven’t shown any attempt to search for a solution or any other effort. – Ole V.V. May 03 '20 at 07:42

3 Answers3

3

Using the Java 8 Time API, you need to use TemporalAdjusters, e.g. like this:

LocalDate today = LocalDate.now();
DayOfWeek dayOfWeek = today.getDayOfWeek();
LocalDate nextMonth = today.plusMonths(1);
LocalDate first = nextMonth.with(TemporalAdjusters.firstInMonth(dayOfWeek));
LocalDate last = first.with(TemporalAdjusters.lastInMonth(dayOfWeek));

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE, MMM d, uuuu");
System.out.println(today.format(fmt));
System.out.println(first.format(fmt));
System.out.println(last.format(fmt));

Output

Sun, May 3, 2020
Sun, Jun 7, 2020
Sun, Jun 28, 2020
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

You could try this

public static void main(String... args) {
    LocalDate now = LocalDate.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE: dd-MM-yyyy");

    System.out.println("Now " + now.format(dtf));
    System.out.println("First " + getFirstDayOfNextMonth(now).format(dtf));
    System.out.println("Last " + getLastDayOfNextMonth(now).format(dtf));
}

public static LocalDate getLastDayOfNextMonth(LocalDate localDate) {
    return localDate
            .plusMonths(1)
            .with(TemporalAdjusters.lastInMonth(localDate.getDayOfWeek()));
}


public static LocalDate getFirstDayOfNextMonth(LocalDate localDate) {
    return localDate
            .plusMonths(1)
            .with(TemporalAdjusters.firstInMonth(localDate.getDayOfWeek()));
}

Output:

Now Sun: 03-05-2020
First Sun: 07-06-2020
Last Sun: 28-06-2020
Lungu Daniel
  • 816
  • 2
  • 8
  • 15
-1

i think, you can just continue with adding today's date with 7 day and go on. İt would be generic.

for example:

get date : 03.05.2020 sunday

add 7 day to it, 10.05.2020 (is it on next month? no, son add another 7)

17.05.2020 (is it on next month? no, son add another 7)

etc.

You can easily do this adding things with Date interface

  • While the basic idea is fine, I recommend you don’t use `java.util.Date`. One, it’s a poorly designed and long outdated class; two, it won’t work for the job, at least not unless you twist its arms harder on its back than it can tolerate. – Ole V.V. May 03 '20 at 10:09