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