【问题标题】:How to get the date of next specified day of week如何获取下一个指定星期几的日期
【发布时间】:2015-01-01 11:10:44
【问题描述】:

我必须根据当前日期计算下一个日期。例如,假设今天是“2014 年 11 月 5 日”,并且用户输入了第一个星期一之类的输入。因此,根据用户输入,如果本月的第一个星期一已经过去,那么我必须找出下个月的第一个星期一。如果当月的第一个星期一还没有到来,那么我必须找出当月的日期。

我正在使用 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.
 }

无法继续进行。寻求您的帮助。

【问题讨论】:

  • 显示你尝试过的代码。
  • @Jens:我已经用代码更新了我的问题。请帮助我。

标签: java date java.util.calendar


【解决方案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 &lt; currentNoOfWeek 而不是 noOfWeek &gt; currentNoOfWeek((noOfWeek == currentNoOfWeek) &amp;&amp; dayOfWeek &gt; currentDayOfWeek) 而不是 ((noOfWeek == currentNoOfWeek) &amp;&amp; dayOfWeek &lt; currentDayOfWeek)&lt;&gt; 是相反的)。


现在是 calculateNextValidDate 方法,这是您的问题所在。我的做法如下:

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

这给了我以下代码:

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

还请注意我在本文第一个代码部分的主方法中添加的// TODO:。如果用户输入无效(例如负周或 dayOfMonth),它可能会通过 while 循环太多次。我让你来验证用户输入。

【讨论】:

  • 非常感谢。你拯救了我的一天。干杯..!!
【解决方案2】:

这是一个想法。继续滚动您的 Calendar 实例一天 [cal.add(DAY, 1)] 每次迭代(即,包裹在循环中),每次检查您是否处于指定为输入的 Day/Week 中。一旦满足此条件,完成并提取日期。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2019-06-09
    • 1970-01-01
    • 2011-03-02
    • 2017-12-16
    • 2011-04-08
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多