How to get date of first Wednesday of month java calendar
It works
- set start date
- go to next month
- transition to the first day of the month
- add the days until we get to 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();
}
source to share
In Java 8 and later, we can use the java.time classes including LocalDate
and TemporalAdjusters
and DayOfWeek
enumeration. See Tutorial .
LocalDate firstSundayOfNextMonth = LocalDate
.now()
.with( TemporalAdjusters.firstDayOfNextMonth() )
.with( TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY) );
source to share
Perhaps you need this code:
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())));
Exit to date: Apr 2012 04
. For the Apr 2012 04
output is Apr 2012 04
. For the Apr 2012 05
exit May 2012 02
.
source to share
This code will answer your question by providing a little more functionality. Using this you can specify a specific business day and it will return the first occurrence of the specified weekday in the next month. It also avoids looping and checking each day until you find the right one. I converted this 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();
}
source to share
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;
}
source to share