nha*_*man 46 java android jodatime
我想在一周的开头和当周的结束之间创建一个间隔.
我从以下答案中借鉴了以下代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
但现在我希望周一LocalDateTime
是00:00:00,周日LocalDateTime
是23:59:59.我该怎么做?
Jod*_*hen 141
您可以使用以下withTime
方法:
d.withTime(0, 0, 0, 0);
d.withTime(23, 59, 59, 999);
Run Code Online (Sandbox Code Playgroud)
与彼得的答案相同,但更短.
The*_*ger 84
也是一个简单的方法
d.millisOfDay().withMaximumValue();
Pet*_*son 23
怎么样:
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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
45839 次 |
最近记录: |