如何获取下一个指定星期几的日期

时间:2014-11-05 09:58:04

标签: java date java.util.calendar

我必须根据当前日期计算下一个日期。例如,今天说的是'05 -NOV-2014'并且用户提供了像第一个周一那样的输入。所以根据用户输入,如果当月的第一个星期一已经过去,那么我必须找出下个月的第一个星期一。如果第一个星期一尚未到来,那么我必须找出当月的日期。

我正在使用java.util.Calendar类。

//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.
 }

无法继续下去。寻求你的帮助。

2 个答案:

答案 0 :(得分:1)

这是一个非常酷的问题,这是我提出的解决方案和我的方法:

首先你有什么:

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);
}

有些注意事项:我已将if和if-else同时放入单个if中,因为在这两种情况下你都希望进入下个月,并且还将它作为一个单独的方法(使其成为一个单独的方法只是我自己的偏好,保持事物的结构和组织。)

我注意到的另一件事是if和else-if中的错误。它应该是noOfWeek < currentNoOfWeek而不是noOfWeek > currentNoOfWeek((noOfWeek == currentNoOfWeek) && dayOfWeek > currentDayOfWeek)而不是((noOfWeek == currentNoOfWeek) && dayOfWeek < currentDayOfWeek)<>会被颠倒过来。


现在是calculateNextValidDate-method,这是你的问题所在。我的方法如下:

  • 从下个月的第一天开始
  • 转到本月正确的一周
  • 然后去本周正确的一天

这给了我以下代码:

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();
}

此代码给了我以下输出(2014年11月5日星期三 - 以3 [Tuesday]2作为输入):

Tue Dec 09 17:05:42 CET 2014

另请注意我在此帖子的第一个代码部分的main方法中添加了// TODO:。如果用户输入无效(例如负周或dayOfMonth),它可能会经历while循环太多次。我留给你验证用户输入。

答案 1 :(得分:0)

这是一个想法。每次迭代(即循环换行)一天继续滚动日历实例[cal.add(DAY,1)],每次检查您是否在指定为输入的日/周内。完成并在满足此条件后提取日期。

另外,在“cal”变量声明之后替换对cal.get()的Calendar.getInstance()。get()的调用。