【发布时间】:2017-05-05 17:48:12
【问题描述】:
我需要做一个返回下个月实际日期位置的方法。
for today (20/12/2016)
I need to call this method whit today date
The return given must to be (17/01/2016)
This method must return the third Tuesday of the next month
Is the 4 week of this month, but I need the Third Tuesday.
我尝试使用 Calendar.MONTH、Calendar.WEEK_OF_MONTH、Calendar.DAY_OF_WEEK,但我无法获得第三个,我总是获得第四个。
类似这样的事情:
public static Date getNextMonthDayOfWeel(Date d) {
Calendar c = Calendar.getInstance();
c.setTime(d);
int week = c.get(Calendar.WEEK_OF_MONTH);
int day = c.get(Calendar.DAY_OF_WEEK);
//// this block
if(!firstWeekOfMonthHad(day)){
week++;
}
//// this block
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.add(Calendar.MONTH, 1);
c.set(Calendar.WEEK_OF_MONTH, week);
c.set(Calendar.DAY_OF_WEEK, day);
return c.getTime();
}
如果一个月的第一周有具体的星期几,我怎么能得到?
【问题讨论】:
-
请向我们展示您的尝试并提供minimal reproducible example
-
那么,是第三个星期二还是他 X 星期一。这些是不同的日期。