使用java.time查找一年中的第一天

时间:2016-05-12 13:35:24

标签: java date java-time

我需要使用Java 8 Date and Time API(java.time)找到一年中最后一周的第一天,并最终找到了这个解决方案:

LocalDate date = LocalDate.of(2016, 2, 17);
LocalDate lastWeekOfYear = LocalDate.of(date.getYear() + 1, 1, 7)
    .with(WeekFields.ISO.weekOfYear(), 1)
    .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusDays(7);

此解决方案找到明年的第一周,如有必要,将星期几调整为星期一,并将其​​移回7天。是否有更聪明的方法来实现相同的结果?

4 个答案:

答案 0 :(得分:4)

正如所建议的那样,我将自己回答这个问题(从评论中稍作改进),因为似乎没有一个明显更简单的解决方案。

LocalDate date = LocalDate.of(2016, 2, 17);
LocalDate lastWeekOfYear = LocalDate.of(date.getYear() + 1, 1, 7)
    .with(WeekFields.ISO.weekOfWeekBasedYear(), 1)
    .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusWeeks(1);

答案 1 :(得分:3)

根据您的评论更新:

您可以从约会开始,到达一年的最后一天,然后回到星期一。获取此结果的WeekBasedYear并接受它,如果weekBasedYear与日期相同,则返回上一个星期一并接受结果:

LocalDate day = LocalDate.of(2012, 2, 17);
LocalDate result =  day.with(TemporalAdjusters.lastDayOfYear())
                            .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));      

//if day and result weekBasedYear are in the same weekYear then result else go back to previous Mondays
result = result.get(WeekFields.ISO.weekBasedYear())== day.getYear()? result :
                result.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));

System.out.println(result);

输出:

2012-12-24
希望这可以提供帮助。

答案 2 :(得分:0)

我认为OP找到的最后一个解决方案仍然不正确,因为我们正在ISO周工作日的上下文中移动,而不是普通的日历日期。因此,表达式LocalDate.of(date.getYear() + 1, 1, 7)通常在年边界处是错误的(请参阅示例方法lastWeekOfYearOld(...))。相反,我们必须添加一个基于周的年份,而不是一个日历年(约束到1月到12月),现在已经在第二种方法lastWeekOfYearCorrect(...)中使用了。我还根据年周字段的上下文敏感值范围显示另一个正确的替代方法(参见方法lastWeekOfYearAlternative(...)

public static void main(String[] args) {
    System.out.println(
              lastWeekOfYearOld(LocalDate.of(2015, 12, 31))); // 2015-12-28 (OK)
    System.out.println(
              lastWeekOfYearOld(LocalDate.of(2016, 1, 1))); // 2016-12-26 (Wrong)

    System.out.println(
              lastWeekOfYearCorrect(LocalDate.of(2015, 12, 31))); // 2015-12-28 (OK)
    System.out.println(
              lastWeekOfYearCorrect(LocalDate.of(2016, 1, 1))); // 2015-12-28 (OK)

    System.out.println(
              lastWeekOfYearAlternative(LocalDate.of(2015, 12, 31))); // 2015-12-28 (OK)
    System.out.println(
              lastWeekOfYearAlternative(LocalDate.of(2016, 1, 1))); // 2015-12-28 (OK)
}

private static LocalDate lastWeekOfYearOld(LocalDate date) {
    return LocalDate.of(date.getYear() + 1, 1, 7)
         .with(WeekFields.ISO.weekOfWeekBasedYear(), 1)
         .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusWeeks(1);
}

private static LocalDate lastWeekOfYearCorrect(LocalDate date) {
    date =
      date.plus(1, IsoFields.WEEK_BASED_YEARS)
          .with(DayOfWeek.MONDAY)
          .with(WeekFields.ISO.weekOfWeekBasedYear(), 1);
    return date.minusWeeks(1);
}

private static LocalDate lastWeekOfYearAlternative(LocalDate date) {
    TemporalField woyField = WeekFields.ISO.weekOfWeekBasedYear();
    date = date.with(woyField, woyField.rangeRefinedBy(date).getMaximum());
    return date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
}

答案 3 :(得分:0)

TL;博士

org.threeten.extra.YearWeek.from(  // Represents an entire week at a time, using standard ISO 8601 definition of “week”. 
    LocalDate.of( 2016 , Month.FEBRUARY , 17 )
)                                  // Returns a `YearWeek` object to represent the week containing that date. 
.plusYears( 1 )                    // Move to following year.
.withWeek( 1 )                     // Move to the first week of that following year.
.minusWeeks( 1 )                   // Move to the prior week, which would be the last week of our desired week-based-year.
.atDay(                            // Determine the date of a particular day-of-week within that week.
    DayOfWeek.MONDAY               // Use the handy `java.time.DayOfWeek` enum predefining seven objects, one for each day of the week.
)                                  // Returns a `LocalDate` object.
.toString()                        // Generate a String representing this `LocalDate` value in standard ISO 8601 format.
  

2016年12月26日

YearWeek

ThreeTen-Extra库提供了额外的日期时间类,补充了 java.time 中的那些。

该图书馆的YearWeek课程适合您的目的。此类使用标准ISO 8601定义表示基于周的一年的整周。

获取当前的一周。

YearWeek yw = YearWeek.now( ZoneId.of( "Africa/Tunis" ) ) ;  // Get the current year-week as seen in the wall-clock time of people inhabiting a particular region (a time zone). 

或者获取特定日期的YearWeek

LocalDate ld = LocalDate.of( 2016 , Month.FEBRUARY , 17 ) ;  // Feb 2, 2016.
YearWeek yw = YearWeek.from( ld ) ;

确定今年的最后一周数字,52或53.这里我们使用三元运算符而不是更长的if-else语句。

int w = yw.is53WeekYear​() ? 53 : 52 ;  // Determine last week number, either 52 or 53.

转到本周为基础的上周。

YearWeek ywLastOfYear = yw.withWeek( w ) ; // Produce a new `YearWeek` object based on the original’s values but for some adjustment - here we use the same year but use a different week number.

最后,获取该周第一天的日期,第一天是星期一。

LocalDate firstDayOfLastYearWeek = ywLastOfYear.atDay( DayOfWeek.MONDAY ) ; 

这是一种方法。有关其他方法,请参阅上面的 tl; dr 部分。