看来你作为一个起点,需要在每周某一天(您的例子中的星期日或星期一)开始的第一整周。
这可能与一些来实现这样的:
import static java.time.temporal.TemporalAdjusters.nextOrSame;
public static LocalDate getStartingDate(int year, int weekNo, DayOfWeek weekStart) {
//should check that arguments are valid etc.
return Year.of(year).atDay(1).with(nextOrSame(weekStart)).plusDays((weekNo - 1) * 7);
}
或作为一种替代方案:
return Year.of(year).atDay(1).with(ALIGNED_WEEK_OF_YEAR, weekNo).with(nextOrSame(weekStart));
你这样称呼它:
import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SUNDAY;
System.out.println(getStartingDate(2016, 1, SUNDAY)); //2016-01-03
System.out.println(getStartingDate(2016, 1, MONDAY)); //2016-01-04
你为什么要接受'年'作为一个字符串?您希望的星期编号系统是否精确到ISO-8601?我还建议'LocalDate'是更合适的返回类型。 –