TbDate - added functions to add years, months, days, etc.
This commit is contained in:
parent
8e13e3b5de
commit
3c137dec77
@ -37,6 +37,7 @@ import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Locale;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TbDate implements Serializable, Cloneable {
|
||||
|
||||
@ -481,6 +482,43 @@ public class TbDate implements Serializable, Cloneable {
|
||||
instant = Instant.ofEpochMilli(dateMilliSecond);
|
||||
}
|
||||
|
||||
public void addDays(int days) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusDays(days));
|
||||
}
|
||||
|
||||
public void addYears(int years) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusYears(years));
|
||||
}
|
||||
|
||||
public void addMonths(int months) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusMonths(months));
|
||||
}
|
||||
|
||||
public void addWeeks(int weeks) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusWeeks(weeks));
|
||||
}
|
||||
|
||||
public void addHours(int hours) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusHours(hours));
|
||||
}
|
||||
|
||||
public void addMinutes(int minutes) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusMinutes(minutes));
|
||||
}
|
||||
|
||||
public void addSeconds(int seconds) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusSeconds(seconds));
|
||||
}
|
||||
|
||||
public void addNanos(long nanos) {
|
||||
adjustTime(zonedDateTime -> zonedDateTime.plusNanos(nanos));
|
||||
}
|
||||
|
||||
private void adjustTime(Function<ZonedDateTime, ZonedDateTime> adjuster) {
|
||||
ZonedDateTime zonedDateTime = adjuster.apply(getZonedDateTime());
|
||||
this.instant = zonedDateTime.toInstant();
|
||||
}
|
||||
|
||||
public ZoneOffset getLocaleZoneOffset(Instant... instants){
|
||||
return ZoneId.systemDefault().getRules().getOffset(instants.length > 0 ? instants[0] : this.instant);
|
||||
}
|
||||
|
||||
@ -885,4 +885,64 @@ class TbDateTest {
|
||||
Assertions.assertNotNull(serializedTbDate);
|
||||
Assertions.assertEquals(expectedDate.toString(), serializedTbDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddFunctions() {
|
||||
TbDate d = new TbDate(2024, 1, 1, 10, 0, 0, 0);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addYears(1);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addYears(-2);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addMonths(2);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addMonths(10);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addMonths(-13);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addWeeks(4);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addWeeks(-5);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addDays(6);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addDays(45);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addDays(-50);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addHours(23);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addHours(-47);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addMinutes(59);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addMinutes(-60);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addSeconds(59);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addSeconds(-60);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addNanos(999999);
|
||||
testResultChangeDateTime(d);
|
||||
|
||||
d.addNanos(-1000000);
|
||||
testResultChangeDateTime(d);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user