时间:下个星期五怎么去? [英] Time: How to get the next friday?
本文介绍了时间:下个星期五怎么去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Joda-Time API 获得下一个星期五.
How can I get the next friday with the Joda-Time API.
LocalDate
<今天的/a> 是 today
.在我看来,您必须决定在本周星期五之前还是之后.看这个方法:
The LocalDate
of today is today
. It looks to me you have to decide whever you are before or after the friday of the current week. See this method:
private LocalDate calcNextFriday(LocalDate d) {
LocalDate friday = d.dayOfWeek().setCopy(5);
if (d.isBefore(friday)) {
return d.dayOfWeek().setCopy(5);
} else {
return d.plusWeeks(1).dayOfWeek().setCopy(5);
}
}
是否可以缩短或使用单线?
Is it possible to do it shorter or with a oneliner?
PS:请不要建议我使用 JDK 的日期/时间内容.Joda-Time 是一个更好的 API.
Java 8 introduces java.time package (Tutorial) which is even better.
推荐答案
java.time
使用 Java 内置的 java.time 框架8 及更高版本(教程)您可以使用 TemporalAdjusters
以获取 next 或 previous 星期几.
java.time
With the java.time framework built into Java 8 and later (Tutorial) you can use TemporalAdjusters
to get next or previous day-of-week.
private LocalDate calcNextFriday(LocalDate d) {
return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
}
这篇关于时间:下个星期五怎么去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文