is there a way to get all weeks of a year plus start and ending days of every week ? (With Joda-Time)
例如(2012年):
week : 21 start: 21.05.2012 ending : 27.05.12
谢谢你的帮忙
is there a way to get all weeks of a year plus start and ending days of every week ? (With Joda-Time)
例如(2012年):
week : 21 start: 21.05.2012 ending : 27.05.12
谢谢你的帮忙
尝试此 :
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
Period weekPeriod = new Period().withWeeks(1);
DateTime startDate = new DateTime(2012, 1, 1, 0, 0, 0, 0 );
DateTime endDate = new DateTime(2013, 1, 1, 0, 0, 0, 0 );
Interval i = new Interval(startDate, weekPeriod );
while(i.getEnd().isBefore( endDate)) {
System.out.println( "week : " + i.getStart().getWeekOfWeekyear()
+ " start: " + df.format( i.getStart().toDate() )
+ " ending: " + df.format( i.getEnd().minusMillis(1).toDate()));
i = new Interval(i.getStart().plus(weekPeriod), weekPeriod);
}
请注意,周数从52开始,然后从1-51开始,因为1月1日不是星期日。
如果您想看每周周一至周日的日期:
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
Period weekPeriod = new Period().withWeeks(1);
DateTime startDate = new DateTime(2012, 1, 1, 0, 0, 0, 0 );
while(startDate.getDayOfWeek() != DateTimeConstants.MONDAY) {
startDate = startDate.plusDays(1);
}
DateTime endDate = new DateTime(2013, 1, 1, 0, 0, 0, 0);
Interval i = new Interval(startDate, weekPeriod);
while(i.getStart().isBefore(endDate)) {
System.out.println("week : " + i.getStart().getWeekOfWeekyear()
+ " start: " + df.format(i.getStart().toDate())
+ " ending: " + df.format(i.getEnd().minusMillis(1).toDate()));
i = new Interval(i.getStart().plus(weekPeriod), weekPeriod);
}
fYI,"http://www.joda.org/joda-time/"rel="nofollow noreferrer" >Joda-Time 项目现在在
您可以以不同的方式定义一个星期。