【发布时间】:2013-01-15 10:26:47
【问题描述】:
例如:日期范围为 2013 年 1 月 3 日至 2013 年 10 月 3 日。 在上述范围内,获取每 3 个月第一周的所有工作日。结果是这样的:
Thu Jan 03 00:00:00 ICT 2013
Fri Jan 04 00:00:00 ICT 2013
Sat Jan 05 00:00:00 ICT 2013
Sun Jan 06 00:00:00 ICT 2013
Mon Jan 07 00:00:00 ICT 2013
Mon Apr 01 00:00:00 ICT 2013
Tue Apr 02 00:00:00 ICT 2013
Wed Apr 03 00:00:00 ICT 2013
Thu Apr 04 00:00:00 ICT 2013
Fri Apr 05 00:00:00 ICT 2013
Sat Apr 06 00:00:00 ICT 2013
Sun Apr 07 00:00:00 ICT 2013
Mon Jul 01 00:00:00 ICT 2013
Tue Jul 02 00:00:00 ICT 2013
Wed Jul 03 00:00:00 ICT 2013
Thu Jul 04 00:00:00 ICT 2013
Fri Jul 05 00:00:00 ICT 2013
Sat Jul 06 00:00:00 ICT 2013
Sun Jul 07 00:00:00 ICT 2013
Tue Oct 01 00:00:00 ICT 2013
Wed Oct 02 00:00:00 ICT 2013
我已经找到了一种方法,但是代码看起来很丑,所以我想知道是否有任何方法会更优雅?感谢您的帮助,我真的很感激。
// set up the startCal and endCal
String fmtShowTime = "ddMMyy"; // TODO: move to configurations
SimpleDateFormat sdf = new SimpleDateFormat(fmtShowTime);
TimeZone defaultZone = TimeZone.getDefault();
sdf.setTimeZone(defaultZone);
String fromTime = "030113";
String toTime = "031013";
Date startDate = sdf.parse(fromTime);
Date endDate = sdf.parse(toTime);
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
// set up the condition for week and month
Integer week = Integer.parseInt("1"); // first week
Integer monthPeriod = Integer.parseInt("3"); // every 3 months
Integer counter = 0;
while (startCal.getTimeInMillis() < endCal.getTimeInMillis()) {
if (startCal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == week) {
// check if the current day in loop is the day of first week in month
if (monthPeriod == 1) {
// get for every month
for (int i = 0; i < 7; i++) {
if (startCal.get(Calendar.DAY_OF_WEEK_IN_MONTH) != week || startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
// break for case like Tuesday, January 8th (2nd Tuesday of January) or October 4th (range breached)
break;
}
System.out.println(startCal.getTime());
// move 1 more day
startCal.add(Calendar.DAY_OF_MONTH, 1);
}
}
else {
counter++;
if (counter % monthPeriod == 1) {
// I used counter mod monthPeriod to check if 3 months has passed in the loop, 1%3 = 1, 4%3 = 1 and so on
for (int i = 0; i < 7; i++) {
if (startCal.get(Calendar.DAY_OF_WEEK_IN_MONTH) != week || startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
// break for case like Tuesday, January 8th (2nd Tuesday of January) or October 4th (range breached)
break;
}
System.out.println(startCal.getTime());
// move 1 more day
startCal.add(Calendar.DAY_OF_MONTH, 1);
}
} else {
//if it's the first week of a month but not 3 months has passed yet then I forward 7 days to pass the check for first week of the month
startCal.add(Calendar.DAY_OF_MONTH, 7);
}
}
}
startCal.add(Calendar.DAY_OF_MONTH, 1);
}
P.S 对不起,如果代码让你眼睛受伤,我知道这很糟糕 :)
【问题讨论】:
-
while (startCal.getTimeInMillis() Calendar 实现了
Comparable,因此这可以写成startCal.compareTo(endCal) < 0(甚至,因为方法是那里,startCal.before(endCal) -
如果范围是从 2013 年 1 月 15 日到 2013 年 10 月 15 日呢?此外,标题显示“2 个月”,而您的问题显示“3 个月”。
-
@fge 感谢您的帮助。使用它会使它看起来更漂亮一点。
-
@sp00m 抱歉 2 或 3 个月之间的错误,但这没关系,因为如果您查看代码,有一个变量用于定义月份之间的持续时间,如果是 1 月 15 日,则为 $monthPeriod到 10 月 15 日,然后从 2 月 1 日开始,接下来的 3 个月,5 月 1 日,依此类推