在乔达蒂姆能计算一个月的周数吗?
我需要这样的东西:
月:7月
月:8月
我知道在乔达时间,我可以得到这样的一年中的一周:
new LocalDate().weekOfWeekYear()
但我不知道如何得到相关的日期。
发布于 2012-08-08 13:19:47
要检索一周的范围,只需创建一个指向一周的第一天和最后一天的对象,然后从其中提取月份的日期。
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九月”之类的东西。
发布于 2013-03-14 18:40:08
修正一些变量:
int weekOfYear = 32;
LocalDate firstDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(1);
LocalDate lastDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(6);
int weekStart = firstDay.getDayOfMonth();
int weekEnd = lastDay.getDayOfMonth();
System.out.println("Week of Year "+weekOfYear+"; "+weekStart+"-"+weekEnd+" "+month);发布于 2015-06-10 02:26:45
我的解决方案,考虑到其他的答案
public List<Pair<LocalDate, LocalDate>> getWeeksInMonth(LocalDate data) {
int firstWeekInMonth = data.withDayOfMonth(1).getWeekOfWeekyear();
int lastWeekInMonth = data.dayOfMonth().withMaximumValue().getWeekOfWeekyear();
List<Pair<LocalDate, LocalDate>> weeks = new cicero.minhasfinancas.util.array.ArrayList<>();
while (firstWeekInMonth <= lastWeekInMonth) {
LocalDate firstDay = new LocalDate().withWeekOfWeekyear(firstWeekInMonth).withDayOfWeek(1);
LocalDate lastDay = new LocalDate().withWeekOfWeekyear(firstWeekInMonth).withDayOfWeek(7);
weeks.add(new Pair<>(firstDay, lastDay));
firstWeekInMonth++;
}
return weeks;
}
public class Pair<V1, V2>{
V1 first;
V2 second;
public Pair(V1 first, V2 second) {
this.first = first;
this.second = second;
}
public V1 getFirst() {
return first;
}
public void setFirst(V1 first) {
this.first = first;
}
public V2 getSecond() {
return second;
}
public void setSecond(V2 second) {
this.second = second;
}
}https://stackoverflow.com/questions/11865123
复制相似问题