Get the number of weeks between two Dates.

number of days between two dates excel
how do you calculate weeks between two dates
number of months between two dates - excel
date calculator
date calculator online
how many weeks in a year
how many weeks from 23 march till now
find out how many days between two dates

Im working in a project and I got two types in Date. I want to calculate the number of weeks between these two dates. The dates can be in diffrent years. Is there any good solution for this?

I have tried to implemenent this with Joda-time which was suggested in other topics..

Im not familar with this library, but I tried to do something like this:

public static int getNumberOfWeeks(Date f, Date l){
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(f);
    c2.setTime(l);
    DateTime start = new DateTime(c1.YEAR, c1.MONTH, c1.DAY_OF_MONTH, 0, 0, 0, 0);
    DateTime end   = new DateTime(c2.YEAR, c2.MONTH, c2.DAY_OF_MONTH, 0, 0, 0, 0);
    Interval interval = new Interval(start, end);
    Period p = interval.toPeriod();
    return p.getWeeks();
}

But this is completely wrong... any suggestions ?

It is pretty easy with joda time:

DateTime dateTime1 = new DateTime(date1);
DateTime dateTime2 = new DateTime(date2);

int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();

Date Duration Calculator: Days Between Dates, How many days, months, and years are there between two dates? Time and Date Duration – Calculate duration, with both date and time included are 1 billion seconds old; Week Number Calculator – Find the week number for any date� To calculate the number of weeks between two dates divide the number of days between two dates by number 7. The result is the number of weeks between two dates.

Updating answer to account for Java 8

// TechTrip - ASSUMPTION d1 is earlier than d2
// leave that for exercise
public static long getFullWeeks(Calendar d1, Calendar d2){

    Instant d1i = Instant.ofEpochMilli(d1.getTimeInMillis());
    Instant d2i = Instant.ofEpochMilli(d2.getTimeInMillis());

    LocalDateTime startDate = LocalDateTime.ofInstant(d1i, ZoneId.systemDefault());
    LocalDateTime endDate = LocalDateTime.ofInstant(d2i, ZoneId.systemDefault());

    return ChronoUnit.WEEKS.between(startDate, endDate);
}

Online calculator: How many weeks are between two dates, The image above demonstrates a formula that calculates the number of complete weeks between two dates. Column B and column C contains� You can calculate how many weeks (and days) old you are or simply how many weeks duration it is between two dates. Simply choose the dates below and we will show the number of weeks and days between them. From Sunday, 9 August 2020 to Wednesday, 31 March 2021, there are 33 weeks and 3 days

Using the date arithmetic in java.util.Calendar:

public static int getWeeksBetween (Date a, Date b) {

    if (b.before(a)) {
        return -getWeeksBetween(b, a);
    }
    a = resetTime(a);
    b = resetTime(b);

    Calendar cal = new GregorianCalendar();
    cal.setTime(a);
    int weeks = 0;
    while (cal.getTime().before(b)) {
        // add another week
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        weeks++;
    }
    return weeks;
}

public static Date resetTime (Date d) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(d);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

How to calculate the number of weeks between dates, If you would like reminders for an upcoming date you can create a My Weeks Until account and get reminders 1 week, 1 month, etc before the date. Find the date� Tips: If you want to get the number of full weeks between two dates, please apply this formula: =ROUNDDOWN((DATEDIF(A2, B2, "d") / 7), 0) =INT((B2-A2)/7) Calculate Various Difference Between Two Dates In Days, Weeks, Months, Years etc.

How Many Weeks between two dates?, #2 – Calculate the Number of Weeks Between Dates. To determine how many weeks elapsed between two dates, we can use a simple formula to� Counting the Number of Weeks Between Two Dates in Excel To calculate the number of weeks between 2 dates you need to divide the formula by 7 which calculates the number of days. Just follow the procedure of the below picture to find the number of weeks. Here you can see the decimal numbers as the number is not rounded up.

Calendar a = new GregorianCalendar(2002,1,22);
    Calendar b = new GregorianCalendar(2002,1,28);
    System.out.println(a.get(Calendar.WEEK_OF_YEAR));
    System.out.println(b.get(Calendar.WEEK_OF_YEAR)); 
   int weeks = b.get(Calendar.WEEK_OF_YEAR)-a.get(Calendar.WEEK_OF_YEAR);
    System.out.println(weeks);

try this must work

    Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2007, 01, 10);
calendar2.set(2007, 07, 01);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
int diffWeeks = (int)diff / (7*24 * 60 * 60 * 1000);

3 Tips for Calculating Week Numbers from Dates in Excel, Weeks Between Two Dates Calculator. Week Calculator: Inputs. First Date: Second Date:� How many days, months, and years are there between two dates? Count Days Add Days Workdays Add Workdays Weekday Week № From and including: Saturday, July 25, 2020 To, but not including Saturday, July 25, 2020

Week Calculator: How Many Weeks Between Dates? – DQYDJ, You can calculate Date duration in the form of total weeks by subtracting date and then Duration: 1:29 Posted: Nov 14, 2016 The Duration Calculator calculates the number of days, months and years between two dates.

How To Count the number of days, weeks, months or years between , The “d” returns the number of full days between the two dates. Difference in weeks. =(DATEDIF(D13,E13,"d")/7) and. In this example, the� To calculate the number of workdays between two dates, you can use the NETWORKDAYS function. NETWORKDAYS automatically excludes weekends, and it can optionally exclude a custom list of holidays as well.

Calculate Total Weeks Between Two Dates - Excel 2013, If I have two dates (ex. '8/18/2008' and '9/26/2008'), what is the best way to get the number of days between these two dates? Stack Overflow.

Comments
  • FYI: The Joda-Time project is now in maintenance mode. Its creator, Stephen Colebourne, went on to lead JSR 310 defining the java.time classes built into Java 8+. See Tutorial by Oracle.
  • Correct answer, but becoming outdated. The creators of Joda-Time have said we should migrate to the java.time framework.
  • True in this case, but not an option, if you rely on the Interval class for example. There is no alternative for that in java.time.
  • You will find an Interval class in the ThreeTen-Extra project that extends java.time. ThreeTen-Extra also serves as a proving ground for possible future additions to java.time classes. But true that Joda-Time and java.time are not at 100% feature parity. While largely equivalent, each has a few features the other lacks.
  • About Threeten-Extra, it seems to be almost dormant (see low activity, not even a replacement for Jodas PeriodFormatter exists yet). Another alternative having interval support etc. can be found in my lib Time4J which is interoperable with java.time-package and can be used as extension, too.
  • Note that the start date is always inclusive while the end date is exclusive.
  • here if i am giving 28 Feb 2018 and 2 march 2019, it is calculating 1 week, it should calculate 2 weeks. do you help to me calculate ?
  • @IrfanNasim exactly what I am looking for.did you find any solution?
  • Will this function work if the dates are in different years?
  • You need to align the dates to the first day of week, otherwise it will return 1 for different days in the same week
  • Something like cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
  • Normally I don't like to downvote, but do here for following reasons: a) your code throws an UnsupportedTemporalTypeException. b) Introducing Threeten-Extra as solution is silly because Java-8 already supports the calculation of elapsed weeks, see the correct answer of @TechTrip here, so what is the point to add an extra dependency only for that purpose? c) Threeten-Extra has no "official" status. Future enhancements going into java.time happen directly on OpenJDK (see all the scheduled features for Java-9 in java.time-component).
  • @MenoHochschild Thanks for the criticisms. With those in mind, I revamped my Answer.
  • OK, I have undone the downvote after your corrections.