tl;dr
Testing a pair of LocalDate
objects by using org.threeten.extra.LocalDateRange
class with a TemporalAdjuster
named ta
that determines the first day of the week.
LocalDateRange.of(
localDate1.with( ta ) ,
localDate1.with( ta ).plusWeeks( 1 )
)
.contains(
localDate2
)
ThreeTen-Extra
The ThreeTen-Extra project adds additional functionality to the java.time classes built into Java (defined by JSR 310).
Among the added classes is LocalDateRange
. This class represents a span-of-time attached to the timeline. In other words, a pair of LocalDate
objects. This class offers handy methods for comparing, such as overlaps
, contains
, and abuts
. Here we need equals
.
Define a method where you pass your two dates plus the first day of the week (a DayOfWeek
enum object).
We use a TemporalAdjuster
found in the TemporalAdjusters
class to determine the date of the first day of the week. Then add a week to get the end of the week (per Half-Open definition of a span-of-time).
We could be cute and check to see if both dates are the same. A worthy check if such a case is likely often in your app.
For week 2, let's use a one-liner as alternative syntax.
public boolean inSameWeek ( LocalDate localDate1 , LocalDate localDate2 , DayOfWeek firstDayOfWeek ) {
Objects.requireNonNull( localDate1 ) ; // … ditto for other arguments.
if( localDate1.isEqual( localDate2 ) ) { return true ; }
TemporalAdjuster ta = TemporalAdjusters.previousOrSame( firstDayOfWeek ) ;
LocalDate weekStart1 = localDate1.with( ta ) ;
LocalDate weekStop1 = weekStart1.plusWeeks( 1 ) ;
LocalDateRange week1 = LocalDateRange.of( weekStart1 , weekStop1 ) ;
LocalDateRange week2 =
LocalDateRange.of(
localDate2.with( ta ) ,
localDate2.with( ta ).plusWeeks( 1 )
)
;
// Compare the weeks.
return week1.equals( week2 ) ;
}
Or even more compact.
public boolean inSameWeek ( LocalDate localDate1 , LocalDate localDate2 , DayOfWeek firstDayOfWeek ) {
Objects.requireNonNull( localDate1 ) ; // … ditto for other arguments.
if( localDate1.isEqual( localDate2 ) ) { return true ; }
TemporalAdjuster ta = TemporalAdjusters.previousOrSame( firstDayOfWeek ) ;
return
LocalDateRange.of(
localDate1.with( ta ) ,
localDate1.with( ta ).plusWeeks( 1 )
)
.equals(
LocalDateRange.of(
localDate2.with( ta ) ,
localDate2.with( ta ).plusWeeks( 1 )
)
)
;
}
We really do not need that second week. We only care if the first determined week contains the second passed date argument.
public boolean inSameWeek ( LocalDate localDate1 , LocalDate localDate2 , DayOfWeek firstDayOfWeek ) {
Objects.requireNonNull( localDate1 ) ; // … ditto for other arguments.
if( localDate1.isEqual( localDate2 ) ) { return true ; }
TemporalAdjuster ta = TemporalAdjusters.previousOrSame( firstDayOfWeek ) ;
return
LocalDateRange.of(
localDate1.with( ta ) ,
localDate1.with( ta ).plusWeeks( 1 )
)
.contains(
localDate2
)
;
}
Use it.
LocalDate localDate = LocalDate.of( 2019 , Month.JANUARY , 23 ) ;
LocalDate today = LocalDate.now( ZoneId.of( "Africa/Tunis") ) ;
boolean sameWeek = this.inSameWeek( localDate , today , DayOfWeek.SUNDAY ) ;
Caveat: I ran none of this code.