获取两个date之间的星期数。

我在一个项目中工作,我有两种types的date。 我想计算这两个date之间的星期数。 date可以在不同的年份。 有没有什么好的解决scheme呢?

我试图用Joda时间来实现这一点,这是在其他主题中提出的。

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

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

但是这是完全错误的…有什么build议吗?

乔达时间非常简单:

 DateTime dateTime1 = new DateTime(date1); DateTime dateTime2 = new DateTime(date2); int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks(); 

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

java.util.Calendar使用date算术:

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

看看下面的文章: Java – 计算两个date之间的差异

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

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

希望这可能会帮助你

你可以这样做:

 // 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,表示两个date之间的星期数。

乔达时间计算两个date的持续时间,在某些情况下可能不符合我们的要求。 我有一个与乔达时间的方法来计算两个date之间的自然周。 希望它可以帮助你。 如果您不使用Joda Time,则可以使用日历修改代码以执行相同的操作。

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

包括年份差异。 这对我工作:)

 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之前的一周

你明白了

java.time

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

ThreeTen-EXTRA

ThreeTen-Extra项目是Java 8及更高版本中构build的java.time框架的官方扩展。 这个项目是未来可能添加到java.time的一个certificate基础,本意是有用的。

Weeksclass

该项目包括一个Weeks代表几个星期。 它不仅可以计算,还可以作为types安全的对象在代码中使用。 这样的使用也有助于使您的代码自我logging。

您可以通过在Weeks.between方法中提供一对时间点来实例化。 那些时间点可以是实现java.time.temporal.Temporal的任何东西,包括InstantLocalDateOffsetDateTimeZonedDateTimeYearYearMonth等等。

您的java.util.Date对象可以很容易地转换为Instant对象,UTC时间轴上的时刻,分辨率为纳秒。 查看添加到旧date时间类的新方法。 对于从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.