I want to get the last date for each of the X weeks that start from date Y.
for example the start date is 12/9/2011
and the test date is 15/9/2011
(which is in the first week starting from the start date).
Using the start and test dates, I would like to get the week number (week 1, week 2, week 3, etc...). For this case 15/9/2011
is in week 1 and the last date for week 1 is 18/9
.
I wrote some java code to get the last dates for every week until week 19 starting from the start date, but it returns the wrong date.
How can i get the correct date?
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));
}
}
The problem is you use integer variables to calculate some numbers that can exceed the Integer.MAX_VALUE
Check the part of this code inside the parenthesis, you will notice it actual is -1875767296 in week 4, that's why your date decrease as you increase the week number
long lastDate = (weekNumber * 7 * 24 * 60 * 60 * 1000) + milliseconds1;
Since Java 8, you can use the …Exact
methods in Math
class to trap for an ArithmeticException
on integer overflow.