It was a pretty cool problem you have, and this is the solution I came up with and my approach:
First what you had:
public static void main(String[] args) {
// TODO: validate user-input
// Input by user:
int inputDayOfWeek = 3; // Tuesday
int inputWeekOfMonth = 2;
if(isInNextMonth(inputDayOfWeek, inputWeekOfMonth)){
Date outputDate = calculateNextValidDate(inputDayOfWeek, inputWeekOfMonth);
// Do something with the outputDate
System.out.println(outputDate.toString());
}
}
private static boolean isInNextMonth(int inputDayOfWeek, int inputWeekOfMonth){
// Current day:
Calendar cal = Calendar.getInstance();
int currentDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int currentWeekOfMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
// The date has gone past in the current month
// OR though it's the same week of the month, the day of the week is past the current day of the week
return inputWeekOfMonth < currentWeekOfMonth || ((inputWeekOfMonth == currentWeekOfMonth) && inputDayOfWeek < currentDayOfWeek);
}
Some things to note: I've put both the if and if-else into a single if, since in both cases you want to go to the next month, and also made it a separate method (making it a separate method is just a preference of myself, to keep things structured and organized).
Another thing I've noticed was an error in your if and else-if. It should be noOfWeek < currentNoOfWeek
instead of noOfWeek > currentNoOfWeek
and ((noOfWeek == currentNoOfWeek) && dayOfWeek > currentDayOfWeek)
instead of ((noOfWeek == currentNoOfWeek) && dayOfWeek < currentDayOfWeek)
(the <
and >
are reversed).
Now the calculateNextValidDate-method, which was where your problem lies. My approach is as follows:
- Start on the first day of the next month
- Go to the correct week of this month
- Then go to the correct day of this week
This gave me the following code:
private static Date calculateNextValidDate(int inputDayOfWeek, int inputWeekOfMonth){
// Set the first day of the next month as starting position:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
// Now first go to the correct week of this month
int weekOfNextMonth = 1;
while(weekOfNextMonth < inputWeekOfMonth){
// Raise by a week
cal.add(Calendar.DAY_OF_MONTH, 7);
weekOfNextMonth++;
}
// Now that we have the correct week of this month,
// we get the correct day
while(cal.get(Calendar.DAY_OF_WEEK) != inputDayOfWeek){
// Raise by a day
cal.add(Calendar.DAY_OF_MONTH, 1);
}
return cal.getTime();
}
This code gave me the following output (on Wednesday 5th of November 2014 - with 3 [Tuesday]
and 2
as input):
Tue Dec 09 17:05:42 CET 2014
Also note the // TODO:
I've added in the main-method of the first code-part of this post. If the user-input is invalid (like a negative week or dayOfMonth for example), it can go through the while-loops too many times. I leave it up to you to validate the user-input.