我想获取从日期 Y 开始的 X 周的最后日期。
例如,开始日期是
12/9/2011
,测试日期是 15/9/2011
(从开始日期开始的第一周)。
使用开始日期和测试日期,我想获取周数(第 1 周、第 2 周、第 3 周等)。对于本例,
15/9/2011
位于第 1 周,第 1 周的最后日期是 18/9
。
我编写了一些java代码来获取从开始日期开始直到第19周的每周的最后日期,但它返回了错误的日期。
如何获得正确的日期?
import java.util.Calendar;
public class WeekCalculation {
public static void main(String [] args) {
Calendar dateStart = Calendar.getInstance();
Calendar testDate1 = Calendar.getInstance();
Calendar testDate2 = Calendar.getInstance();
Calendar testDate3 = Calendar.getInstance();
Calendar testDate4 = Calendar.getInstance();
Calendar testDate5 = Calendar.getInstance();
Calendar testDate6 = Calendar.getInstance();
Calendar testDate7 = Calendar.getInstance();
Calendar testDate8 = Calendar.getInstance();
Calendar testDate9 = Calendar.getInstance();
Calendar testDate10 = Calendar.getInstance();
Calendar testDate11 = Calendar.getInstance();
Calendar testDate12 = Calendar.getInstance();
Calendar testDate13= Calendar.getInstance();
Calendar testDate14 = Calendar.getInstance();
Calendar testDate15 = Calendar.getInstance();
Calendar testDate16 = Calendar.getInstance();
Calendar testDate17 = Calendar.getInstance();
Calendar testDate18 = Calendar.getInstance();
Calendar testDate19 = Calendar.getInstance();
dateStart.set(2011, 8, 12);
testDate1.set(2011, 8, 15);
testDate2.set(2011, 8, 20);
testDate3.set(2011, 9, 1);
testDate4.set(2011, 9, 7);
testDate5.set(2011, 9, 13);
testDate6.set(2011, 9, 20);
testDate7.set(2011, 9, 29);
testDate8.set(2011, 10, 4);
testDate9.set(2011, 10, 12);
testDate10.set(2011, 10, 17);
testDate11.set(2011, 10, 25);
testDate12.set(2011, 11, 2);
testDate13.set(2011, 11, 8);
testDate14.set(2011, 11, 17);
testDate15.set(2011, 11, 20);
testDate16.set(2011, 11, 27);
testDate17.set(2012, 0, 7);
testDate18.set(2012, 0, 13);
testDate19.set(2012, 0, 20);
int weekNumber1 = findWeekNumber(dateStart, testDate1);
int weekNumber2 = findWeekNumber(dateStart, testDate2);
int weekNumber3 = findWeekNumber(dateStart, testDate3);
int weekNumber4 = findWeekNumber(dateStart, testDate4);
int weekNumber5 = findWeekNumber(dateStart, testDate5);
int weekNumber6 = findWeekNumber(dateStart, testDate6);
int weekNumber7 = findWeekNumber(dateStart, testDate7);
int weekNumber8 = findWeekNumber(dateStart, testDate8);
int weekNumber9 = findWeekNumber(dateStart, testDate9);
int weekNumber10 = findWeekNumber(dateStart, testDate10);
int weekNumber11 = findWeekNumber(dateStart, testDate11);
int weekNumber12 = findWeekNumber(dateStart, testDate12);
int weekNumber13 = findWeekNumber(dateStart, testDate13);
int weekNumber14 = findWeekNumber(dateStart, testDate14);
int weekNumber15 = findWeekNumber(dateStart, testDate15);
int weekNumber16 = findWeekNumber(dateStart, testDate16);
int weekNumber17 = findWeekNumber(dateStart, testDate17);
int weekNumber18 = findWeekNumber(dateStart, testDate18);
int weekNumber19 = findWeekNumber(dateStart, testDate19);
findDayLeftInTheWeek(dateStart, weekNumber1, testDate1);
findDayLeftInTheWeek(dateStart, weekNumber2, testDate2);
findDayLeftInTheWeek(dateStart, weekNumber3, testDate3);
findDayLeftInTheWeek(dateStart, weekNumber4, testDate4);
findDayLeftInTheWeek(dateStart, (weekNumber5), testDate5);
findDayLeftInTheWeek(dateStart, weekNumber6, testDate6);
findDayLeftInTheWeek(dateStart, weekNumber7, testDate7);
findDayLeftInTheWeek(dateStart, weekNumber8, testDate8);
findDayLeftInTheWeek(dateStart, weekNumber9, testDate9);
findDayLeftInTheWeek(dateStart, weekNumber10, testDate10);
findDayLeftInTheWeek(dateStart, (weekNumber11), testDate11);
findDayLeftInTheWeek(dateStart, weekNumber12, testDate12);
findDayLeftInTheWeek(dateStart, weekNumber13, testDate13);
findDayLeftInTheWeek(dateStart, weekNumber14, testDate14);
findDayLeftInTheWeek(dateStart, weekNumber15, testDate15);
findDayLeftInTheWeek(dateStart, weekNumber16, testDate16);
findDayLeftInTheWeek(dateStart, (weekNumber17), testDate17);
findDayLeftInTheWeek(dateStart, weekNumber18, testDate18);
findDayLeftInTheWeek(dateStart, weekNumber19, testDate19);
}
public static int findWeekNumber(Calendar startDate, Calendar currentDate) {
long milliseconds1 = startDate.getTimeInMillis();
long milliseconds2 = currentDate.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;;
long diffDays = diff / (24 * 60 * 60 * 1000);
return (int)(diffDays/7)+1;
}
public static void findDayLeftInTheWeek(Calendar startDate, int weekNumber, Calendar currentDate) {
Calendar lastDateOfTheWeek = Calendar.getInstance();
long milliseconds1 = startDate.getTimeInMillis();
long oneday = 24*60*60*1000;
long lastDate = (weekNumber * 7 * 24 * 60 * 60 * 1000)+milliseconds1;
lastDateOfTheWeek.setTimeInMillis(lastDate-oneday);
System.out.println("last date of the week "+weekNumber+ " " +lastDateOfTheWeek.get(Calendar.DATE)+"/"+(lastDateOfTheWeek.get(Calendar.MONTH)+1)+"/"+lastDateOfTheWeek.get(Calendar.YEAR));
}
}
问题是你使用整数变量来计算一些可能超过
Integer.MAX_VALUE
的数字
检查括号内的代码部分,您会注意到第 4 周的实际值为 -1875767296,这就是为什么您的日期随着周数的增加而减少
long lastDate = (weekNumber * 7 * 24 * 60 * 60 * 1000) + milliseconds1;
在 Java 的更高版本中,您可以使用
…Exact
类中的
Math
方法来捕获ArithmeticException
整数溢出。
看起来通过使用他们定义的常量应该更容易做到这一点,因为您不需要毫秒粒度。你可以尝试这样的事情:
public static int findWeekNumber(Calendar startDate, Calendar currentDate) {
int startWeek = startDate.get(Calendar.WEEK_OF_YEAR);
int endWeek = currentDate.get(Calendar.WEEK_OF_YEAR);
return endWeek - startWeek;
}
public static void findDayLeftInTheWeek(Calendar startDate, int weekNumber, Calendar currentDate) {
// Make a copy of currentDate, so that it doesn't get changed.
Calendar lastDay = Calendar.getInstance();
lastDay.setTime(currentDate.getTime());
// Determine the last day of the week. From your example stating that September 18,
// 2011 was the last day of the week, it looks like you're considering Sunday to be
// the last day of the week.
switch(currentDate.get(Calendar.DAY_OF_WEEK)) {
case Calendar.MONDAY:
lastDay.add(Calendar.DAY, 6);
break;
case Calendar.TUESDAY:
lastDay.add(Calendar.DAY, 5);
break;
case Calendar.WEDNESDAY:
lastDay.add(Calendar.DAY, 4);
break;
case Calendar.THURSDAY:
lastDay.add(Calendar.DAY, 3);
break;
case Calendar.FRIDAY:
lastDay.add(Calendar.DAY, 2);
break;
case Calendar.SATURDAY:
lastDay.add(Calendar.DAY, 1);
break;
}
// You can format the print appropriately; this is just an example.
System.out.println("The last day of week " + weekNumber + " is " + lastDay.toString());
}