1

Hello at first i would like to note that i have found several posts with the same question here, but NONE of them worked for me. i am creating alarm clock application for android and the last thing i need is: get the date of the nearest certain day in week.

I have found several algorithms here and i will also copy one here :

import java.util.Calendar;

public class NextWednesday {
    public static Calendar nextDayOfWeek(int dow) {
        Calendar date = Calendar.getInstance();
        int diff = dow - date.get(Calendar.DAY_OF_WEEK);
        if (!(diff > 0)) {
            diff += 7;
        }
        date.add(Calendar.DAY_OF_MONTH, diff);
        return date;
    }
    public static void main(String[] args) {
        System.out.printf(
            "%ta, %<tb %<te, %<tY",
            nextDayOfWeek(Calendar.WEDNESDAY) // this can contain any of the 7 days in week
        );
    }
}
  • Today is tuesday in my country

  • If i put wednesday in the function it returns me the wednesday that is in the next week, but thats not correct.

  • This algorithm automatically looks at the following week no matter if its just monday and theres whole week before you, it jumps to the next week and does its job but thats not correct, i need to implement the same behaviour but it must start from today.

Example: Today is Monday, i am looking for wednesday

  • Correct output: Date of wednesday in this week.
  • Uncorrect output: Date of wednesday in the next week.

I hope its clear enough.

Community
  • 1
  • 1
uplnypan
  • 47
  • 4
  • What exactly is your question? – shmosel Oct 11 '16 at 18:26
  • what does `System.out.printf("%ta, % – JitterbugChew Oct 11 '16 at 18:29
  • I need to get the date of the nearest requested day the user opens application and sets the alarm clock to be fired on each wednesday so for example today its tuesday and i need to pass to the AlarmManager the nearest date of the wednesday, this function returns me the nearest wednesday in the NEXT week i need to get the one thats in this week and if the wednedday in this week has already passed they yeah , get the one from next week, but why it skips the wednesday when its just tuesday and gets the one from next week – uplnypan Oct 11 '16 at 18:30
  • 1
    Its working fine for me, prints out tomorrows date. – StarSweeper Oct 11 '16 at 18:32
  • *FYI:* `!(diff > 0)` is better written as `diff <= 0`. --- Anyway, I get `Wed, Oct 12, 2016`, so **unable to reproduce**. – Andreas Oct 11 '16 at 18:36
  • for me it prints the date of the wednesday of the next week. thats sad. oh – uplnypan Oct 11 '16 at 18:38
  • Did you try to print the value of `Calendar.getInstance().getTime()`? Maybe it's not what you think it is. I get `Tue Oct 11 14:41:28 EDT 2016`, so next Wednesday is tomorrow. – Andreas Oct 11 '16 at 18:39
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 09 '18 at 00:46

2 Answers2

0

Okay, algorithm works correctly, i made a simple mistake, i was passing wrong day to the function, i passed the current day, not the one that was chosen by the user

uplnypan
  • 47
  • 4
0

If you wish to keep it simple.

 public Date getNextDate(int dayOfWeek) {
      Calendar c = Calendar.getInstance();
      for ( int i = 0; i < 7; i++ ) {
         if ( c.get(Calendar.DAY_OF_WEEK) == dayOfWeek ) {
            return c.getTime();
         } else {
            c.add(Calendar.DAY_OF_WEEK, 1);
         }
      }
      return c.getTime();
}
Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 09 '18 at 00:45