要检索一周的范围,只需创建一个指向一周的第一天和最后一天的对象,然后从中提取月份中的某一天。
int weekOfYear = 32;
LocalDate firstDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(1);
LocalDate lastDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(7);
System.out.println("Week of Year "+weekOfYear+"; "+firstDay.toString("d MMM")+" - "+lastDay.toString("d MMM"));
您还可以像这样提取日期:
int weekStart = firstDay.getDayOfMonth();
int weekEnd = lastDay.getDayOfMonth();
然后,您也可以使用相同的技术来检索一个月中的周数。
int firstWeekInMonth = new LocalDate().withMonthOfYear(month).withDayOfMonth(1).getWeekOfYear();
int lastWeekInMonth = new LocalDate().withMonthOfYear(month).dayOfMonth().withMaximalValue().getWeekOfYear();
可能您可能希望将开始日期和结束日期限制在该月的范围内,否则您可能会得到“30 - 5 Sep”之类的信息。