在jodatime中计算一个月的周数 [英] calculate weeks for one month in jodatime
问题描述
是否有可能在jodatime中计算一个月的周数?
Is it possible to calculate the weeks of one month in jodatime?
我需要这样的事情:
月份:7月
- 27年周; 1-7。 7月
- 28年周; 9-14。 7月
- 29年周; 16-21。 7月
- 30年周; 23-31。 7月
- Week of Year 27; 1-7. July
- Week of Year 28; 9-14. July
- Week of Year 29; 16-21. July
- Week of Year 30; 23-31. July
月份:8月
- 31年周; 1-4。 8月
- 32年周; 6-11。 8月
- 33年周; 13-18。 8月
- 34年周; 20-25。 8月
- 35年周; 27-31。 8月
- Week of Year 31; 1-4. Aug
- Week of Year 32; 6-11. Aug
- Week of Year 33; 13-18. Aug
- Week of Year 34; 20-25. Aug
- Week of Year 35; 27-31. Aug
我知道我可以像这样在joda时间获得一周的一周:
I know that i can get the week of Year in joda time like this:
new LocalDate()。weekOfWeekYear()
但我不知道如何获得相关日期。
But i don´t know how to get the related dates.
推荐答案
要检索一周的范围,只需创建一个指向该日期的第一天和最后一天的对象。一周,然后从它那里拉出一天。
To retrieve the range of a week, just create an object pointing to the first and last day of the week, then just pull the day of month from it.
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"));
您还可以提取这样的日期:
You can also extract the day like this:
int weekStart = firstDay.getDayOfMonth();
int weekEnd = lastDay.getDayOfMonth();
然后您可以使用相同的技术检索一个月内的周数。
You can then use the same technique to retrieve the weeks in a month as well.
int firstWeekInMonth = new LocalDate().withMonthOfYear(month).withDayOfMonth(1).getWeekOfYear();
int lastWeekInMonth = new LocalDate().withMonthOfYear(month).dayOfMonth().withMaximalValue().getWeekOfYear();
可能您可能希望限制开始日期和结束日期保持在月份范围内,否则你可以得到像'30 - 9 Sep'这样的东西。
Probably you might want to limit the start and end-dates to stay within the range of the month, otherwise you could get things like '30 - 5 Sep'.
这篇关于在jodatime中计算一个月的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!