gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:12:41 29 4
gpt4 key购买 nike

我必须根据当前日期计算下一个日期。例如,假设今天是“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.
}

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

最佳答案

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

首先是你拥有的:

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 方法,这是您的问题所在。我的做法如下:

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

这给了我以下代码:

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 循环太多次。我让您来验证用户输入。

关于java - 如何获取下一个指定星期几的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26754164/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com