如何获取下一个特定日期的日期 [英] How to get the date of next specified day of week
问题描述
我必须根据当前日期计算下一个日期。例如,今天说'05 -NOV-2014',用户已经输入了像星期一一样的输入。所以根据用户输入,如果当月的第一个星期一已经过去了,那么我必须找出下个月的第一个星期一。如果第一个星期一到现在还没到,那么我必须在当月找出日期。
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.
我正在使用java.util.Calendar类。
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.
推荐答案
这是一个非常酷的问题,这是我想出的解决方案,我的方法:
It was a pretty cool problem you have, and this is the solution I came up with and my approach:
首先你有什么:
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成为一个单一的,因为在这两种情况下,你想去下个月,并且使它成为一个单独的方法(使一个单独的方法仅仅是我自己的偏好,保持结构和组织)。
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).
我注意到的另一件事是你的if和else-if中的错误。它应该是 noOfWeek< currentNoOfWeek
而不是 noOfWeek>
((noOfWeek == currentNoOfWeek)&& dayOfWeek> currentDayOfWeek)而不是((noOfWeek = = currentNoOfWeek)&& dayOfWeek< currentDayOfWeek)
(<
和>
反转)。
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).
现在,calculateNextValidDate方法是您的问题所在。我的方法如下:
Now the calculateNextValidDate-method, which was where your problem lies. My approach is as follows:
- 从下个月的第一天开始
- 转到本月正确的一周
- 然后转到本周正确的日子
给了我以下代码:
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 [星期二]
和 2
作为输入):
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
另外请注意 // TODO:
我已经添加了这篇文章的第一个代码的主要方法。如果用户输入无效(例如,一个负数周或一天),它可以遍历while循环太多次。我留给你来验证用户输入。
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.
这篇关于如何获取下一个特定日期的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!