3

How would I get the date of the next first Wednesday of the month using the java calendar class. For example:

Today(24/03/2012) the next first Wednesday will be 04/04/2012
On(05/04/2012) the next first Wednesday will be 02/05/2012

thanks.

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
user667430
  • 1,487
  • 8
  • 38
  • 73
  • Refer this [Get Last Friday of Month in Java](http://stackoverflow.com/questions/76223/get-last-friday-of-month-in-java) – Siva Charan Mar 24 '12 at 18:40

6 Answers6

4

In Java 8 and later, we can use the java.time classes including LocalDate and TemporalAdjusters, and the DayOfWeek enum. See Tutorial.

LocalDate firstSundayOfNextMonth = LocalDate
              .now()
              .with( TemporalAdjusters.firstDayOfNextMonth() )
              .with( TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY) );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
KayV
  • 12,987
  • 11
  • 98
  • 148
3

This works

  • set start date
  • move to next month
  • move to first day in month
  • add days until we get to a wednesday

Code

import static java.util.Calendar.*;

public static void main(String[] args) {
    System.out.println(getNextMonthFirstWed(new Date(112, 3 - 1, 24)));
    System.out.println(getNextMonthFirstWed(new Date(112, 4 - 1, 05)));
}

private static Date getNextMonthFirstWed(Date date) {
    Calendar c = getInstance();
    c.setTime(date);
    c.add(MONTH, 1);
    c.set(DAY_OF_MONTH, 1);

    // search until wednesday
    while (c.get(DAY_OF_WEEK) != WEDNESDAY) {
        c.add(DAY_OF_MONTH, 1);
    }
    return c.getTime();
}
Adam
  • 35,919
  • 9
  • 100
  • 137
2

This code will answer your question while providing a little more functionality. Using this, you can give a specific week day and it will return the first occurrence of the specified week day in the next month. This also avoids looping through and checking each day until you find the correct one. I converted this over from some groovy code that I am currently working with. Hope this helps!

private static Date getNextMonthFirstWeekDayOccurence(Date date, int weekDay) {
            //Get Calendar and move it to the first day of the next month
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);

            //Calculate the difference between the days of the week
            int dayDifference = calendar.get(Calendar.DAY_OF_WEEK) - weekDay;

            if (dayDifference < 0) { //Implies (calendar.get(Calendar.DAY_OF_WEEK) < weekDay)
                calendar.add(Calendar.DAY_OF_MONTH, Math.abs(dayDifference));
            } else if (dayDifference > 0) { //Implies (calendar.get(Calendar.DAY_OF_WEEK) > weekDay)
                def daysToAdd = calendar.get(Calendar.DAY_OF_WEEK) - dayDifference;
                calendar.add(Calendar.DAY_OF_MONTH, daysToAdd);
            }
            return calendar.getTime();
    }
2

Maybe this code is what you need:

    Calendar calendar = Calendar.getInstance();
    while (calendar.get(Calendar.DAY_OF_MONTH) > 7 ||
            calendar.get(Calendar.DAY_OF_WEEK) != Calendar.WEDNESDAY) {
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    SimpleDateFormat gm = new SimpleDateFormat("MMM yyyy dd");
    System.out.println(gm.format(new Date(calendar.getTimeInMillis())));

The output for today is: Apr 2012 04. For Apr 2012 04 the output is Apr 2012 04. For Apr 2012 05 the output is May 2012 02.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
0

Or the Joda version:

private static DateTime nextFirstWednesday(DateTime dateTime) {
    while (dateTime.getDayOfMonth() > 7 || dateTime.getDayOfWeek() != DateTimeConstants.WEDNESDAY) {
        dateTime = dateTime.plusDays(1);
    }
    return dateTime;
}
nansen
  • 2,912
  • 1
  • 20
  • 33
  • I'm sure Joda does things Calendar cannot, but your solution looks almost identical to mine, except the chaining syntax on line 1... – Adam Mar 24 '12 at 18:59
  • This code will also not produce the expected result for example for 3rd of April. – Boris Strandjev Mar 24 '12 at 19:04
  • Yep, you were right. Fixed it. And removed the readability claim :) I just think joda should be preferred over util.Date and Calendar in general. I find it less error prone and complicated (I know, that was not the question :)). – nansen Mar 24 '12 at 20:00
-1
public boolean firstWednesdayOfTheMonth() {
        // gets todays date
        Calendar calendar = Calendar.getInstance();
        // get the date
        int date = calendar.get(Calendar.DAY_OF_MONTH);
        // get the day of the week SUNDAY == 1, MONDAY == 2 ....
        int day =   calendar.get(Calendar.DAY_OF_WEEK); 
        // this checks if its a wednesday and the date falls within 8 then it should be the first wednesday
        if (date < 8 &&  day == 4) {
                return true;
        }

            return false;   
    }
Sridhar Iyer
  • 189
  • 1
  • 7