0

How would I find the date of the next given day, given name of day (ex "Wednesday"), from a date (ex 12/9/14) in Java?

For example, how I find the next Wednesday after a date of December 9, 2014.

Connorelsea
  • 2,308
  • 6
  • 26
  • 47
  • 1
    [`Calendar`](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) or if you're using Java 8, something like [`LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) - I suggest you make a search of SO or Google, as this has been numerous times before... – MadProgrammer Dec 10 '14 at 03:40
  • I am aware of the classes available. I do not know how to use them properly, and when I have tried I have failed. When I google this issue I find nothing similar. This is why I have asked here. – Connorelsea Dec 10 '14 at 03:44
  • 2
    [Seems easy enough to find](http://www.coderanch.com/t/385117/java/java/date-Monday) or if your prefer [something like](http://stackoverflow.com/questions/76223/get-last-friday-of-month-in-java/10922864#10922864) – MadProgrammer Dec 10 '14 at 03:46
  • Or [even better](http://stackoverflow.com/questions/21242809/find-next-occurrence-of-a-day-of-week-in-jsr-310) – MadProgrammer Dec 10 '14 at 03:47
  • Thank you, but I really dislike the sarcasm. I'm not trying to waste anyone's time. I have tried to code it myself, and I have tried googling. I wasn't trying to just get someone on here to answer it entirely. – Connorelsea Dec 10 '14 at 03:51
  • Then I would encourage you future to provide you efforts and some of the things you've tried to solve it ;) – MadProgrammer Dec 10 '14 at 03:55
  • I'm sorry you feel that I'm being sarcastic, I can assure it's not, but if you prefer, I'll avoid providing you with suggestions in the future – MadProgrammer Dec 10 '14 at 04:00
  • No, the solutions worked. I just got a bad vibe from the ellipsis and other elements of your typing. Sorry that it is hard to tell over the Internet. Also, I feel kind of awkward using Java 8's time libraries inside of the Date class that I made. Java 8's time classes felt too complicated for what I needed (Planner software) but now I am reconsidering. – Connorelsea Dec 10 '14 at 04:03
  • Can you look at this and see if it is good to do. Or what I should do in general. You seem like you could help me with idealogical things about my program. https://github.com/Elsealabs/ePlanner/blob/master/com/elsealabs/planner/Date.java – Connorelsea Dec 10 '14 at 04:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66533/discussion-between-connorelsea-and-madprogrammer). – Connorelsea Dec 10 '14 at 04:07

1 Answers1

0
    int whichDay= Calendar.WEDNESDAY;

    if(cal.get(Calendar.DAY_OF_WEEK)>= whichDay){
        cal.add(Calendar.DAY_OF_MONTH, 7);
    }
    cal.set(Calendar.DAY_OF_WEEK, whichDay);
yajnesh
  • 2,089
  • 1
  • 23
  • 36
  • Or use JodaTime or Java 8's Time API which provide much better solutions... – MadProgrammer Dec 10 '14 at 03:59
  • Java 8 Time API definitely, but not Joda , date4j is far better. http://www.date4j.net/#Joda – yajnesh Dec 10 '14 at 04:07
  • It (date4j) seems more focused on issues with Date and Databases, but that's just the vibe I got from reading the opening spill – MadProgrammer Dec 10 '14 at 04:11
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 30 '18 at 02:30