java - Joda Time - 获取一年中的所有星期

标签 java jodatime date-arithmetic

有没有办法获取一年中的所有星期以及每周的开始和结束日期? (与 Joda-Time )

像这样(2012 年):

周:21 开始:21.05.2012 结束: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);
}

关于java - Joda Time - 获取一年中的所有星期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10721084/

相关文章:

java - 如何解析包含区域设置的日期字符串

sql - 如何在雪花中将日期截断为每月的第一天?

java - 在 java spring boot 中,图像没有从 excel 插入数据库?

java - 如何使用 java 中的当前和本地夏令时和时区信息将 joda LocalDateTime 对象设置为指定时间

windows - 执行 .sh 文件的 Java 代码

android - 条目名称 'org/joda/time/tz/data/Europe/Dublin' 发生冲突

sql - 使用 postgres 将 Excel 日期格式转换为常规日期

ios - 我应该使用 NSDate 来跟踪到达时间和迟到时间吗?

java - 使用 itext 创建 pdf 的问题

java - playframework:Job中的Request.current