-1
public static int countWeeks() {
    // setting dates
    Calendar calStart = GregorianCalendar.getInstance();
    calStart.set(2014, 8, 30);
    Date dateStart = calStart.getTime();

    Date dateEnd = new Date();

    // count days and weeks
    int diffInDays = Days.daysBetween(new DateTime(dateStart), new DateTime(dateEnd)).getDays(); //         int weekNumber = (int) diffInDays / 7;

    return weekNumber;

}

I'm trying to calculate the number of days and weeks between today and last week but I always get -3 as weekNumber. I have no idea what i'm doing wrong. Thanks in advance.

d4yl1ght
  • 1
  • 2
  • What is this `Days` class? If it is JodaTime, you should explain it since it is not part of Java API – SJuan76 Sep 07 '14 at 08:29
  • `int weekNumber` is commented out? So where is `weekNumber` declared? – Pang Sep 07 '14 at 08:31
  • Also, remember that months in Calendar are 0-based, so you are actually setting the start at setember, 30th. Better use `Calendar.AUGUST` for that. – SJuan76 Sep 07 '14 at 08:33
  • Get the milliseconds, substract end from start. Divide by 1000 * 60 * 60 * 24. Have you day difference. – Hannes Sep 07 '14 at 08:54

4 Answers4

1

First, I will assume that

int weekNumber = (int) diffInDays / 7;

is not commented since otherwise you would get a compilation error.

Now, as explained in my comment, by doing

calStart.set(2014, 8, 30);

You are setting the date at the end of setember, not of august. So, it is 3 weeks ahead of now, so you get a -3. Use the Calendar constants.

calStart.set(2014, Calendar.AUGUST, 30);
SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

You are getting -3 as the weeknumber since you have commented that out so it showing some random value. Also note that 8 shows the September month not Aug since months are 0 based.

So if you are aiming at August month then you may try this:

calStart.set(2014, 7, 30);
                   ^^-- This represents August month
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • If the code was commented he would get a compilation error... – SJuan76 Sep 07 '14 at 08:33
  • @SJuan76:- Yes that makes sense. Then probably the issue is with the month as the gettime() will get the present date and the 8 is the September month as against the Aug which I guess OP is thinking :) – Rahul Tripathi Sep 07 '14 at 08:36
0

You set the startdate to Sep. 30th because te month is zero bases!

See the documentation from java.util.Calendar:

public final void set(int year, int month, int date) Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. Previous values of other calendar fields are retained. If this is not desired, call clear() first. Parameters: year - the value used to set the YEAR calendar field. month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January. date - the value used to set the DAY_OF_MONTH calendar field. See Also:

Jens
  • 67,715
  • 15
  • 98
  • 113
0

If you are on Java 8 you can use the Java time API:

LocalDate start = LocalDate.of(2014, 8, 30);
LocalDate end = LocalDate.now();
long days = ChronoUnit.DAYS.between(start, end);
return (int) days / 7;    
assylias
  • 321,522
  • 82
  • 660
  • 783