Jodatime开始的一天和结束的一天

我想创build一个星期开始和本周结束之间的时间间隔。

我有以下代码,从这个答案借来的:

private LocalDateTime calcNextSunday(LocalDateTime d) { if (d.getDayOfWeek() > DateTimeConstants.SUNDAY) { d = d.plusWeeks(1); } return d.withDayOfWeek(DateTimeConstants.SUNDAY); } private LocalDateTime calcPreviousMonday(LocalDateTime d) { if (d.getDayOfWeek() < DateTimeConstants.MONDAY) { d = d.minusWeeks(1); } return d.withDayOfWeek(DateTimeConstants.MONDAY); } 

但是现在我想让星期一的LocalDateTime在00:00:00,星期天的LocalDateTime在23:59:59。 我将如何做到这一点?

怎么样:

 private LocalDateTime calcNextSunday(LocalDateTime d) { return d.withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withDayOfWeek(DateTimeConstants.SUNDAY); } private LocalDateTime calcPreviousMonday(final LocalDateTime d) { return d.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withDayOfWeek(DateTimeConstants.MONDAY); } 

你可以使用withTime方法:

  d.withTime(0, 0, 0, 0); d.withTime(23, 59, 59, 999); 

和彼得的答案一样,但更短。

也是一个简单的方法

d.millisOfDay().withMaximumValue();