How to get the date of next specified day of week

2019-07-20 20:47发布

问题:

I have to calculate the next date based on the current date. For example, say today is '05-NOV-2014' and user have gave an input like 1st Monday. so based on the user input if the 1st Monday of the current month has already gone past then i have to find out the 1st Monday of next month. if the 1st Monday is yet to come in the current month then i have to find out the date within the current month.

I am using java.util.Calendar class.

//user inputs
int dayOfWeek = 3; // as user has provided 'Tuesday'   
int noOfWeek = 1; // as user provided 1st 

Calendar cal = Calendar.getInstance(); // getting the current instance.
int currentDayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK); // getting the cCurrentDayOfWeek in integer.
int currentNoOfWeek  = Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH); // getting which week it is in the current month


 if(noOfWeek < currentNoOfWeek){
 // the date has gone past in the current month.
 }
 else if ((noOfWeek == currentNoOfWeek) && dayOfWeek < currentDayOfWeek){
 // though the week number is same but as the currentDayOfWeek is greater than the provided day so in this case also  date has gone past in the current month.
 }

No able to proceed further. seeking your help.

回答1:

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.



回答2:

Here's an idea. Continue to roll your Calendar instance one day [cal.add(DAY, 1)] per iteration (i.e., wrap in loop), each time checking that you are in the Day/Week specified as input. Finish and extract the date once such condition is met.

Also, substitute calls to Calendar.getInstance().get() for cal.get() after the "cal" variable declaration.