Get the number of weeks between two Dates.

2020-01-24 05:28发布

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 ?

14条回答
时光不老,我们不散
2楼-- · 2020-01-24 05:52
        Calendar date1 = Calendar.getInstance();
        Calendar date2 = Calendar.getInstance();

        date1.clear();
        date1.set(datePicker1.getYear(), datePicker1.getMonth(),
                datePicker1.getDayOfMonth());
        date2.clear();
        date2.set(datePicker2.getYear(), datePicker2.getMonth(),
                datePicker2.getDayOfMonth());

        long diff = date2.getTimeInMillis() - date1.getTimeInMillis();

        float dayCount = (float) diff / (24 * 60 * 60 * 1000);

        int week = (dayCount / 7) ;

Hope this might Help you

查看更多
Fickle 薄情
3楼-- · 2020-01-24 05:53

public int diffInWeeks(Date start, Date end) { long diffSeconds = (end.getTime() - start.getTime())/1000; return (int)diffSeconds/(60 * 60 * 24 * 7); }

查看更多
做自己的国王
4楼-- · 2020-01-24 05:54

It is pretty easy with joda time:

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

int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
查看更多
干净又极端
5楼-- · 2020-01-24 05:57

Joda Time computes weeks with durations of two dates which may not meet our requirements in some cases. I have a method with Joda Time to compute natural weeks between two dates. Hope it can help you. If you don't use Joda Time, you may modify the code with Calendar to do the same thing.

//Unlike Joda Time Weeks.weeksBetween() that returns whole weeks computed
//from duration, we return natural weeks between two dates based on week of year
public static int weeksBetween(ReadablePartial date1, ReadablePartial date2) {
    int comp = date1.compareTo(date2);
    if (comp == 0) {
        return 0;
    }

    if (comp > 0) {
        ReadablePartial mid = date2;
        date2 = date1;
        date1 = mid;
    }

    int year1 = date1.get(DateTimeFieldType.weekyear());
    int year2 = date2.get(DateTimeFieldType.weekyear());

    if (year1 == year2) {
        return date2.get(DateTimeFieldType.weekOfWeekyear()) - date1.get(DateTimeFieldType.weekOfWeekyear());
    }

    int weeks1 = 0;

    LocalDate lastDay1 = new LocalDate(date1.get(DateTimeFieldType.year()), 12, 31);
    if (lastDay1.getWeekyear() > year1) {
        lastDay1 = lastDay1.minusDays(7);
        weeks1++;
    }

    weeks1 += lastDay1.getWeekOfWeekyear() - date1.get(DateTimeFieldType.weekOfWeekyear());

    int midWeeks = 0;
    for (int i = year1 + 1; i < year2; i++) {
        LocalDate y1 = new LocalDate(i, 1, 1);
        int yearY1 = y1.getWeekyear();
        if (yearY1 < i) {
            y1 = y1.plusDays(7);
            midWeeks++;
        }

        LocalDate y2 = new LocalDate(i, 12, 31);
        int yearY2 = y2.getWeekyear();
        if (yearY2 > i) {
            y2 = y2.minusDays(7);
            midWeeks++;
        }

        midWeeks += y2.getWeekOfWeekyear() - y1.getWeekOfWeekyear();
    }

    int weeks2 = 0;
    LocalDate firstDay2 = new LocalDate(date2.get(DateTimeFieldType.year()), 1, 1);
    if (firstDay2.getWeekyear() < firstDay2.getYear()) {
        firstDay2 = firstDay2.plusDays(7);
        weeks2++;
    }
    weeks2 += date2.get(DateTimeFieldType.weekOfWeekyear()) - firstDay2.getWeekOfWeekyear();

    return weeks1 + midWeeks + weeks2;
}
查看更多
走好不送
6楼-- · 2020-01-24 05:58

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);
}
查看更多
可以哭但决不认输i
7楼-- · 2020-01-24 06:01

You may do it the following way:

// method header not shown
// example dates:
f = new GregorianCalendar(2009,Calendar.AUGUST,1);
l = new GregorianCalendar(2010,Calendar.SEPTEMBER,1);
DateTime start = new DateTime(f);
DateTime end = new DateTime(l);
// Alternative to above - example dates with joda:
// DateTime start = new DateTime(2009,8,1,0,0,0,0);
// DateTime end = new DateTime(2010,9,1,0,0,0,0);
Interval interval = new Interval(start,end);
int weeksBetween = interval.toPeriod(PeriodType.weeks()).getWeeks();
// return weeksBetween;

This should give you an int representing the number of weeks between the two dates.

查看更多
登录 后发表回答