It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




7年前关闭。




我是Joda Time的新手,并希望从已知时间段开始创建相等(x)个间隔。我怎样才能做到这一点?

例如。我有3个小时的时间,想创建3个间隔。每个间隔将是一个小时。

最佳答案

  int x = //...
  DateTime dateFrom = new DateTime(/* ... */);
  DateTime dateTo = new DateTime(/* ... */);
  long singlePart = (dateTo.getMillis() - dateFrom.getMillis()) / x;
  List<Interval> result = new ArrayList<Interval>();

  for (int i = 0; i < x; i++)
  {
     result.add(new Interval(dateFrom.plusMillis((int) singlePart * i), dateFrom.plusMillis((int) singlePart * (i + 1))));
  }

07-28 00:04