获取两个日期之间的周数。

时间:2012-04-01 08:55:58

标签: java date jodatime

我在一个项目中工作,我在Date中有两种类型。我想计算这两个日期之间的周数。日期可能是不同的年份。对此有什么好的解决方案吗?

我试图用其他主题中提出的Joda-time来实现这一点。

我不熟悉这个库,但我尝试做这样的事情:

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();
}

但这完全错了......有什么建议吗?

16 个答案:

答案 0 :(得分:32)

使用joda time非常容易:

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

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

答案 1 :(得分:24)

更新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);
}

答案 2 :(得分:8)

使用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();
}

答案 3 :(得分:2)

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);

尝试这必须工作

    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);

答案 4 :(得分:1)

java.time

java.time框架内置于Java 8及更高版本中。这些新类取代了与最早版本的Java捆绑在一起的旧日期时间类。这些类也取代了非常成功的Joda-Time框架,并由同一个人构建,包括由Stephen Colbourne领导。

ThreeTen-EXTRA

ThreeTen-Extra项目是Java 8及更高版本中内置的java.time框架的官方扩展。该项目是未来可能添加到java.time的试验场,并且本身就是有用的。

Weeks class

该项目包含一个代表若干周的Weeks课程。它不仅可以计算,还可以在您的代码中用作类型安全对象。这样的使用也有助于使您的代码自我记录。

您可以通过Weeks.between方法提供一对时间点来实例化。这些时间点可以是实施java.time.temporal.Temporal的任何内容,包括InstantLocalDateOffsetDateTimeZonedDateTimeYearYearMonth,等等。

您的java.util.Date个对象可以很容易地转换为Instant个对象,时间轴上的时刻以UTC为单位,分辨率为纳秒。查看添加到旧日期时间类的新方法。要从Date更改为Instant,请致电java.util.Date::toInstant

Instant start = utilDateStart.toInstant();
Instant stop = utilDateStop.toInstant();
Weeks weeks = Weeks.between( start , stop );

您可以询问周数。您还可以更多。

int weeksNumber = weeks.getAmount(); // The number of weeks in this Weeks object.

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和& SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

答案 5 :(得分:1)

如果您的要求是,开始日期为2020年4月3日,结束日期为2020年4月7日。两个日期之间的差值为4天。现在,两个日期之间的星期数为1 ,您可以在下面的代码段中使用。

ChronoUnit.WEEKS.between(LocalDate startDate, LocalDate endDate);

但是,如果您的要求像是一周之内是2020年4月3日而另一周是2020年4月7日,那么您希望将两个日期之间的周数设为2,就可以使用下面的代码段。

LocalDate actualStartDate=...
LocalDate actualEndDate=...

LocalDate startDate = actualStartDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)) 

LocalDate endDate = actualEndDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY)) 

long daysBetweenTwoDates = ChronoUnit.DAYS.between(startDate, endDate);
int numberOfWeeks =  (int)Math.ceil(daysBetweenTwoDates/7.0);

在Java 1.8中进行了测试

答案 6 :(得分:1)

以下是我编写的 2 个不基于外部库的方法。
第一种方法是 星期一 是一周的第一天。
第二种方法是当星期日是一周的第一天。

请阅读代码中的注释,可以选择返回两个日期之间的完整周数,
以及两个日期前后剩余天数的分数。

public static int getNumberOfFullWeeks(LocalDate startDate,LocalDate endDate)
{
    int dayBeforeStartOfWeek = 0;
    int daysAfterLastFullWeek = 0;

    if(startDate.getDayOfWeek() != DayOfWeek.MONDAY)
    {
        // get the partial value before loop starting
        dayBeforeStartOfWeek = 7-startDate.getDayOfWeek().getValue() + 1;
    }

    if(endDate.getDayOfWeek() != DayOfWeek.SUNDAY)
    {
        // get the partial value after loop ending
        daysAfterLastFullWeek = endDate.getDayOfWeek().getValue();
    }

    LocalDate d1 = startDate.plusDays(dayBeforeStartOfWeek); // now it is the first day of week;
    LocalDate d2 = endDate.minusDays(daysAfterLastFullWeek); // now it end in the last full week

    // Count how many days there are of full weeks that start on Mon and end in Sun
    // if the startDate and endDate are less than a full week the while loop
    // will not iterate at all because d1 and d2 will be the same date
    LocalDate looper = d1;
    int counter = 1;
    while (looper.isBefore(d2))
    {
        counter++;
        looper = looper.plusDays(1);
    }

    // Counter / 7 will always be an integer that will represents full week
    // because we started to count at Mon and stop counting in Sun
    int fullWeeks = counter / 7;

    System.out.println("Full weeks between dates: "
            + fullWeeks + " Days before the first monday: "
            + dayBeforeStartOfWeek + " "
            + " Days after the last sunday: " + daysAfterLastFullWeek);
    System.out.println(startDate.toString() + " - " + endDate.toString());

    // You can also get a decimal value of the full weeks plus the fraction if the days before
    // and after the full weeks
    float full_weeks_decimal = (float)fullWeeks;
    float fraction = ((float)dayBeforeStartOfWeek + (float)daysAfterLastFullWeek) / 7.0F;
    System.out.println("Full weeks with fraction: " + String.valueOf(fraction + full_weeks_decimal));

    return fullWeeks;
}

public static int getNumberOfFullWeeks_WeekStartAtSunday(LocalDate startDate,LocalDate endDate)
{
    int dayBeforeStartOfWeek = 0;
    int daysAfterLastFullWeek = 0;

    if(startDate.getDayOfWeek() != DayOfWeek.SUNDAY)
    {
        // get the partial value before loop starting
        dayBeforeStartOfWeek = 7-getDayOfWeekBySundayIs0(startDate.getDayOfWeek()) + 1;
    }

    if(endDate.getDayOfWeek() != DayOfWeek.SATURDAY)
    {
        // get the partial value after loop ending
        daysAfterLastFullWeek = 1+getDayOfWeekBySundayIs0(endDate.getDayOfWeek());
    }

    LocalDate d1 = startDate.plusDays(dayBeforeStartOfWeek); // now it is the first day of week;
    LocalDate d2 = endDate.minusDays(daysAfterLastFullWeek); // now it end in the last full week

    // Count how many days there are of full weeks that start on Sun and end in Sat
    // if the startDate and endDate are less than a full week the while loop
    // will not iterate at all because d1 and d2 will be the same date
    LocalDate looper = d1;
    int counter = 1;
    while (looper.isBefore(d2))
    {
        counter++;
        looper = looper.plusDays(1);
    }

    // Counter / 7 will always be an integer that will represents full week
    // because we started to count at Sun and stop counting in Sat
    int fullWeeks = counter / 7;

    System.out.println("Full weeks between dates: "
            + fullWeeks + " Days before the first sunday: "
            + dayBeforeStartOfWeek + " "
            + " Days after the last saturday: " + daysAfterLastFullWeek);
    System.out.println(startDate.toString() + " - " + endDate.toString());

    // You can also get a decimal value of the full weeks plus the fraction if the days before
    // and after the full weeks
    float full_weeks_decimal = (float)fullWeeks;
    float fraction = ((float)dayBeforeStartOfWeek + (float)daysAfterLastFullWeek) / 7.0F;
    System.out.println("Full weeks with fraction: " + String.valueOf(fraction + full_weeks_decimal));

    return fullWeeks;
}

   public static int getDayOfWeekBySundayIs0(DayOfWeek day)
    {
        if(day == DayOfWeek.SUNDAY)
        {
            return 0;
        }
        else
        {
            // NOTE: getValue() is starting to count from 1 and not from 0
            return  day.getValue();
        }
    }

答案 7 :(得分:0)

请查看以下文章:Java - calculate the difference between two dates

daysBetween方法允许您获取日期之间的天数。然后你可以简单地除以7得到整周的数量。

答案 8 :(得分:0)

        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) ;

希望这可以帮助你

答案 9 :(得分:0)

您可以通过以下方式执行此操作:

// 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;

这应该给你一个int代表两个日期之间的周数。

答案 10 :(得分:0)

    int startWeek = c1.get(Calendar.WEEK_OF_YEAR);
    int endWeek = c2.get(Calendar.WEEK_OF_YEAR);    

    int diff = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);

    int deltaYears = 0;
    for(int i = 0;i < diff;i++){
        deltaYears += c1.getWeeksInWeekYear();
        c1.add(Calendar.YEAR, 1);        
    }
    diff = (endWeek + deltaYears) - startWeek;

包括年份差异。 这对我有用:)

答案 11 :(得分:0)

private int weeksBetween(Calendar startDate, Calendar endDate) {
    startDate.set(Calendar.HOUR_OF_DAY, 0);
    startDate.set(Calendar.MINUTE, 0);
    startDate.set(Calendar.SECOND, 0);
    int start = (int)TimeUnit.MILLISECONDS.toDays(
        startDate.getTimeInMillis())
        - startDate.get(Calendar.DAY_OF_WEEK);
    int end = (int)TimeUnit.MILLISECONDS.toDays(
        endDate.getTimeInMillis());
    return (end - start) / 7;
}

如果此方法返回0,则它们在同一周

如果此方法返回1 endDate是startDate之后的一周

如果此方法返回-1,则endDate是startDate之前的一周

你明白了

答案 12 :(得分:0)

如果您想要完整周数,请使用以下方法,其中结束日期是独占的:

public static long weeksBetween(Date date1, Date date2) {
    return WEEKS.between(date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
        date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
  }

如果你想要一个ceil版本,请使用以下内容:

public static long weeksBetween(Date date1, Date date2) {
    long daysBetween = DAYS.between(date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
        date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) + 1;
    return daysBetween / 7 + (daysBetween % 7 == 0 ? 0 : 1);
  }

答案 13 :(得分:0)

在不使用JodaTime的情况下,我能够准确计算两个日历之间的星期数(这涉及leap年等)。

private fun calculateNumberOfWeeks() {
    val calendarFrom = Calendar.getInstance()
    calendarFrom.set(Calendar.HOUR_OF_DAY, 0)
    calendarFrom.set(Calendar.MINUTE, 0)
    calendarFrom.set(Calendar.SECOND, 0)
    calendarFrom.set(Calendar.MILLISECOND, 0)

    val calendarTo = Calendar.getInstance()
    calendarTo.add(Calendar.MONTH, months)
    calendarTo.set(Calendar.HOUR_OF_DAY, 0)
    calendarTo.set(Calendar.MINUTE, 0)
    calendarTo.set(Calendar.SECOND, 0)
    calendarTo.set(Calendar.MILLISECOND, 0)

    var weeks = -1
    while (calendarFrom.timeInMillis < calendarTo.timeInMillis) {
        calendarFrom.add(Calendar.DATE, 7)
        weeks++
        Log.d(Constants.LOG_TAG, "weeks $weeks")
    }
}

答案 14 :(得分:0)

简单的方法

torchvision==0.7.0+cpu

答案 15 :(得分:-1)

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