English 中文(简体)
JodaTime - 一年中各星期
原标题:Joda Time - Get all weeks of a year

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);
}
问题回答

Joda-Time is in maintenance mode

fYI,"http://www.joda.org/joda-time/"rel="nofollow noreferrer" >Joda-Time 项目现在在

Define ‘week’

您可以以不同的方式定义一个星期。

我假设你指的是标准