Refactor TemporalAdjusters (#2266)
This commit is contained in:
parent
1acbe9b162
commit
3e2d766f37
|
@ -10,14 +10,13 @@ public class CustomTemporalAdjuster implements TemporalAdjuster {
|
|||
|
||||
@Override
|
||||
public Temporal adjustInto(Temporal temporal) {
|
||||
DayOfWeek dayOfWeek = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
|
||||
int daysToAdd;
|
||||
if (dayOfWeek == DayOfWeek.FRIDAY)
|
||||
daysToAdd = 3;
|
||||
else if (dayOfWeek == DayOfWeek.SATURDAY)
|
||||
daysToAdd = 2;
|
||||
else
|
||||
daysToAdd = 1;
|
||||
return temporal.plus(daysToAdd, ChronoUnit.DAYS);
|
||||
switch (DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK))) {
|
||||
case FRIDAY:
|
||||
return temporal.plus(3, ChronoUnit.DAYS);
|
||||
case SATURDAY:
|
||||
return temporal.plus(2, ChronoUnit.DAYS);
|
||||
default:
|
||||
return temporal.plus(1, ChronoUnit.DAYS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +1,34 @@
|
|||
package com.baeldung.temporaladjusters;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.temporal.TemporalAdjuster;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
|
||||
import com.baeldung.temporaladjuster.CustomTemporalAdjuster;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.temporaladjuster.CustomTemporalAdjuster;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.temporal.TemporalAdjuster;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CustomTemporalAdjusterTest {
|
||||
|
||||
private static final TemporalAdjuster NEXT_WORKING_DAY = new CustomTemporalAdjuster();
|
||||
|
||||
@Test
|
||||
public void whenAdjustAndImplementInterface_thenNextWorkingDay() {
|
||||
LocalDate localDate = LocalDate.of(2017, 07, 8);
|
||||
CustomTemporalAdjuster temporalAdjuster = new CustomTemporalAdjuster();
|
||||
LocalDate nextWorkingDay = localDate.with(temporalAdjuster);
|
||||
|
||||
Assert.assertEquals("2017-07-10", nextWorkingDay.toString());
|
||||
assertEquals("2017-07-10", nextWorkingDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAdjust_thenNextWorkingDay() {
|
||||
LocalDate localDate = LocalDate.of(2017, 07, 8);
|
||||
TemporalAdjuster temporalAdjuster = NEXT_WORKING_DAY;
|
||||
LocalDate date = localDate.with(temporalAdjuster);
|
||||
LocalDate date = localDate.with(NEXT_WORKING_DAY);
|
||||
|
||||
Assert.assertEquals("2017-07-10", date.toString());
|
||||
assertEquals("2017-07-10", date.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -39,18 +39,6 @@ public class CustomTemporalAdjusterTest {
|
|||
|
||||
String fourteenDaysAfterDate = "2017-07-22";
|
||||
|
||||
Assert.assertEquals(fourteenDaysAfterDate, result.toString());
|
||||
assertEquals(fourteenDaysAfterDate, result.toString());
|
||||
}
|
||||
|
||||
static TemporalAdjuster NEXT_WORKING_DAY = TemporalAdjusters.ofDateAdjuster(date -> {
|
||||
DayOfWeek dayOfWeek = date.getDayOfWeek();
|
||||
int daysToAdd;
|
||||
if (dayOfWeek == DayOfWeek.FRIDAY)
|
||||
daysToAdd = 3;
|
||||
else if (dayOfWeek == DayOfWeek.SATURDAY)
|
||||
daysToAdd = 2;
|
||||
else
|
||||
daysToAdd = 1;
|
||||
return date.plusDays(daysToAdd);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue