有没有一种优雅的方法可以使用JodaTime查找给定日期的星期几?我最初以为setCopy()会是这样,但这会把这一天设定为同一周的特定日期.因此,如果ld是2011-11-27和day是"星期一",则以下函数返回2011-11-21,而不是2011-11-28我想要的.
// Note that "day" can be _any_ day of the week, not just weekdays.
LocalDate getNearestDayOfWeek(LocalDate ld, String day) {
return ld.dayOfWeek().setCopy(day);
}
Run Code Online (Sandbox Code Playgroud)
各种输入的所需输出:
2011-12-04, Monday => 2011-12-05
2011-12-04, Tuesday => 2011-12-06
2011-12-04, Wednesday => 2011-12-07
2011-12-04, Thursday => 2011-12-01
2011-12-04, Friday => 2011-12-02
2011-12-04, Saturday => 2011-12-03
2011-12-04, Sunday => 2011-12-04
2011-12-05, Monday => 2011-12-05
2011-12-05, Tuesday => 2011-12-06
2011-12-05, Wednesday => 2011-12-07
2011-12-05, Thursday => 2011-12-08
2011-12-05, Friday => 2011-12-02
2011-12-05, Saturday => 2011-12-03
2011-12-05, Sunday => 2011-12-04
Run Code Online (Sandbox Code Playgroud)
下面是我提出的解决方案,它适用于我当前情况下的特定约束,但我很乐意帮助找到一个始终有效的完全通用的解决方案.
LocalDate getNearestDayOfWeek(LocalDate ld, String day) {
LocalDate target = ld.dayOfWeek().setCopy(day);
if (ld.getDayOfWeek() > DateTimeConstants.SATURDAY) {
target = target.plusWeeks(1);
}
return target;
}
Run Code Online (Sandbox Code Playgroud)
在Jodatime中,这种事情应该是三行或四行:
/** Given a reference LocalDate and a day of week, eg DateTimeConstants.MONDAY
Returns the nearest date with that day of week */
public static LocalDate getNearestDayOfWeek(LocalDate t0,int dow) {
LocalDate t1 = t0.withDayOfWeek(dow);
LocalDate t2 = t1.isBefore(t0) ? t1.plusWeeks(1) : t1.minusWeeks(1);
return Math.abs(Days.daysBetween(t1, t0).getDays()) <
Math.abs(Days.daysBetween(t2, t0).getDays()) ? t1 : t2;
}
Run Code Online (Sandbox Code Playgroud)
或者更紧凑,更高效:
public static LocalDate getNearestDayOfWeek(LocalDate t0, int dow) {
LocalDate t1 = t0.withDayOfWeek(dow);
if (t1.isBefore(t0.minusDays(3))) return t1.plusWeeks(1);
else if (t1.isAfter(t0.plusDays(3))) return t1.minusWeeks(1);
else return t1;
}
Run Code Online (Sandbox Code Playgroud)
如果你想以星期几的形式传递星期几:
public static LocalDate getNearestDayOfWeek(LocalDate t0, String dow) {
return getNearestDayOfWeek(t0,t0.dayOfWeek().setCopy(dow).getDayOfWeek());
}
Run Code Online (Sandbox Code Playgroud)
例:
// prints 2011-11-28
public static void main(String[] args) throws Exception {
LocalDate today = new LocalDate(2011,11,27);
int dow = DateTimeConstants.MONDAY;
System.out.println(getNearestDayOfWeek(today ,dow ));
}
Run Code Online (Sandbox Code Playgroud)