Skip to content

Dates

Date bugs hide on days a test rarely meets. On 29 February 2012, Microsoft Azure went down because code worked out a certificate’s expiry “by simply taking the current date and adding one to its year”, which on a leap day gave 29 February 2013, a day that doesn’t exist (Azure’s post-mortem). On 31 December 2008, Zune players froze on the 366th day of a leap year. Around New Year, code that formats with the ISO week-year writes the wrong year, since 29 December 2014 is in ISO week 2015-W01. A test with a handful of example dates almost never picks one of these, and a random date over two centuries reaches a given leap day about once in 73,000 tries.

This page shows the date domains, which aim a fifth of their tries at the calendar’s edges; how to draw business days and test the code that counts them; how to time code with a clock that goes backwards; and how to check code that reads and writes spreadsheet dates. Each property is a method in a JUnit test class, with io.proptastic.Proptastic.* imported.

Here is the Azure code, and the property it should keep: a certificate is valid for a year.

Certificates.java
/** A year after it's issued: the same day and month, a year on. */
static LocalDate validTo(LocalDate issued) {
var nextYear = issued.getYear() + 1;
return LocalDate.of(nextYear, issued.getMonth(), issued.getDayOfMonth());
}
DatesTest.java
@Test
void aCertificateIsValidForAYear() {
forAll(dates())
.tries(1_000)
.check(
issued -> {
var expires = Certificates.validTo(issued);
assertEquals(issued.plusYears(1), expires);
});
}
Terminal window
DateTimeException: Invalid date 'February 29' as '2001' is not a leap year
aCertificateIsValidForAYear failed after 18 tries (seed 0x5eed)
shrunk counterexample: issued = 2000-02-29
pin it: .example(LocalDate.of(2000, 2, 29))
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.aCertificateIsValidForAYear'

dates() draws a LocalDate from 1 January 1900 to 31 December 2100. About one try in five it draws a calendar edge instead, and the days around 29 February are one class of them, so a leap day comes up about one try in a hundred, where an even draw would take about one in 1,500. This property runs 1,000 tries to be sure of meeting one; here it did after 18. Whichever leap day failed first, it shrinks to the one nearest 1 January 2001: 29 February 2000. The fix is issued.plusYears(1), which gives 28 February.

Edge Why it’s there
the range’s ends off-by-one at the bounds
a leap year’s 28 February, 29 February and 1 March, and 1900’s and 2100’s end of February the leap day and the days either side, and the century years that aren’t leap
31 December of a leap year the 366th day, Zune’s
the 29th to the 31st of a month plusMonths(1) clamps them, so it can’t be undone
28 December to 4 January the ISO week-year’s seam, years with a week 53 first
quarter ends reporting periods
1969-12-31, 1970-01-01, 2038-01-19, 2038-01-20 where 32-bit seconds turn

A failure shrinks toward Monday 1 January 2001, or the bound nearest it. That day has nothing odd about it: a Monday, ISO week 1 of its own year, in a year that’s neither leap nor a century, so a counterexample that lands there says the date didn’t matter. One that doesn’t says which day did. A failure found at an edge shrinks among its class first, so a leap-day bug stays on 29 February, then leaves the class for any day nearer the anchor that fails: one that every day before mid-2000 causes shrinks to the last of them, not to the month end it was found on.

Bound a domain as you bound numbers, dates().atLeast(LocalDate.of(2026, 1, 1)), with atMost, greaterThan and lessThan too. times(), dateTimes(), instants(), durations() and offsets() draw the other java.time types, and .every(Duration.ofMinutes(15)) keeps a time on a grid. Domains measures how often each draws what.

The month end is another edge. Adding a month and taking it away doesn’t give the day back when the next month is shorter:

DatesTest.java
@Test
void aMonthOnAndBackIsTheSameDay() {
forAll(dates())
.check(
day -> {
var back = day.plusMonths(1).minusMonths(1);
assertEquals(day, back);
});
}
Terminal window
expected: <2001-01-29> but was: <2001-01-28>
aMonthOnAndBackIsTheSameDay failed after 1 try (seed 0x5eed)
shrunk counterexample: day = 2001-01-29
pin it: .example(LocalDate.of(2001, 1, 29))
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.aMonthOnAndBackIsTheSameDay'

A desk’s calendar is weekends and holidays. proptastic never ships holiday data, as rules change at a week’s notice and no test library should vouch for a jurisdiction’s calendar. The holidays are yours: a set, or a function. Here they’re a fixed past year, which can’t go stale:

DatesTest.java
/** The desk's holidays in 2025: a fixed past year, which can't go stale. */
static final Set<LocalDate> HOLIDAYS_2025 =
Set.of(
LocalDate.of(2025, 1, 1),
LocalDate.of(2025, 1, 20),
LocalDate.of(2025, 2, 17),
LocalDate.of(2025, 5, 26),
LocalDate.of(2025, 6, 19),
LocalDate.of(2025, 7, 4),
LocalDate.of(2025, 9, 1),
LocalDate.of(2025, 10, 13),
LocalDate.of(2025, 11, 11),
LocalDate.of(2025, 11, 27),
LocalDate.of(2025, 12, 25));
static final BusinessCalendar DESK =
new BusinessCalendar(Set.of(SATURDAY, SUNDAY), HOLIDAYS_2025);

.excluding(…) leaves days out: days of the week, a set of dates, or a predicate. .on(…) keeps only the days of the week you give it:

DatesTest.java
static DateDomain daysIn2025() {
var first = LocalDate.of(2025, 1, 1);
var last = LocalDate.of(2025, 12, 31);
return dates().atLeast(first).atMost(last);
}
static DateDomain businessDays() {
return daysIn2025().excluding(SATURDAY, SUNDAY).excluding(HOLIDAYS_2025);
}

A calendar library you already trust works the same way, through its own question: with Jollyday, .excluding(day -> holidays.isHoliday(day)), where holidays is its HolidayManager; with Strata, .excluding(calendar::isHoliday) on a HolidayCalendar.

These are the calendar the properties check:

BusinessCalendar.java
record BusinessCalendar(Set<DayOfWeek> weekend, Set<LocalDate> holidays) {
boolean isBusinessDay(LocalDate day) {
return !weekend.contains(day.getDayOfWeek()) && !holidays.contains(day);
}
/** The day itself if it's a business day, else the next one. */
LocalDate following(LocalDate day) {
var next = day;
while (!isBusinessDay(next)) {
next = next.plusDays(1);
}
return next;
}
/** Following, unless that leaves the month: then the business day before. */
LocalDate modifiedFollowing(LocalDate day) {
var next = following(day);
if (next.getMonth() == day.getMonth()) {
return next;
}
var before = day.minusDays(1);
while (!isBusinessDay(before)) {
before = before.minusDays(1);
}
return before;
}
/** {@code days} business days after {@code day}; before it when negative. */
LocalDate plusBusinessDays(LocalDate day, int days) {
var step = Integer.signum(days);
var at = day;
for (int left = Math.abs(days); left > 0; ) {
at = at.plusDays(step);
if (isBusinessDay(at)) {
left--;
}
}
return at;
}
/** The business days in [from, to), negative when to is before from. */
int businessDaysBetween(LocalDate from, LocalDate to) {
if (to.isBefore(from)) {
return -businessDaysBetween(to, from);
}
int count = 0;
for (var day = from; day.isBefore(to); day = day.plusDays(1)) {
if (isBusinessDay(day)) {
count++;
}
}
return count;
}
}

An adjusted day is a business day, keeps its month, and adjusting it again changes nothing:

DatesTest.java
@Test
void anAdjustedDayIsABusinessDayInItsMonth() {
forAll(daysIn2025())
.check(
day -> {
var adjusted = DESK.modifiedFollowing(day);
assertTrue(DESK.isBusinessDay(adjusted));
assertEquals(day.getMonth(), adjusted.getMonth());
assertEquals(adjusted, DESK.modifiedFollowing(adjusted));
});
}

Counting and stepping agree, either way round, and stepping further never lands sooner:

DatesTest.java
@Test
void countingAndSteppingAgree() {
forAll(businessDays(), integers().atLeast(-20).atMost(20))
.check(
(day, steps) -> {
var later = DESK.plusBusinessDays(day, steps);
assertEquals(steps, DESK.businessDaysBetween(day, later));
assertEquals(-steps, DESK.businessDaysBetween(later, day));
});
}
@Test
void steppingFurtherNeverLandsSooner() {
var steps = integers().atLeast(0).atMost(20);
forAll(businessDays(), steps, steps)
.check(
(day, some, more) -> {
var sooner = DESK.plusBusinessDays(day, some);
var later = DESK.plusBusinessDays(day, some + more);
assertThat(later).isAfterOrEqualTo(sooner);
});
}

The counting property draws only business days, and it needs to: stepping forward and back returns to the start only from a business day. Draw the weekends with .on(…) and the property says so:

DatesTest.java
@Test
void steppingThereAndBackReturns() {
var weekends = daysIn2025().on(SATURDAY, SUNDAY);
forAll(weekends, integers().atLeast(1).atMost(5))
.check(
(day, steps) -> {
var there = DESK.plusBusinessDays(day, steps);
assertEquals(day, DESK.plusBusinessDays(there, -steps));
});
}
Terminal window
expected: <2025-01-04> but was: <2025-01-03>
steppingThereAndBackReturns failed after 1 try (seed 0x5eed)
shrunk counterexample:
day = 2025-01-04, steps = 1
fails for any:
day, steps
pin it:
.example(/* day */ LocalDate.of(2025, 1, 4), /* steps */ 1)
rerun:
PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.steppingThereAndBackReturns'

Since 28 May 2024, US trades settle one business day after the trade, T+1, where they settled at T+2 before (SEC). The days whose next day is a day off are the ones that break a settlement date, and they’re among the edges .excluding adds:

DatesTest.java
static LocalDate settle(LocalDate trade) {
return DESK.plusBusinessDays(trade, 1);
}
@Test
void aTradeSettlesOnTheNextBusinessDay() {
forAll(businessDays().atLeast(LocalDate.of(2025, 5, 28)))
.check(
trade -> {
var settles = settle(trade);
assertTrue(DESK.isBusinessDay(settles));
assertEquals(1, DESK.businessDaysBetween(trade, settles));
});
}

On 1 January 2017 a leap second made a measured time negative at Cloudflare, and the negative number crashed a random-number call (Cloudflare). No java.time type can hold second 60, so the way to test for it is a fake clock whose readings are drawn, some of them backwards. Here’s a stopwatch that adds up a clock’s ticks:

DatesTest.java
/** Times a run by adding up the clock's ticks. */
static Duration elapsed(List<Duration> ticks) {
var now = Duration.ZERO;
for (var tick : ticks) {
now = now.plus(tick);
}
return now;
}
DatesTest.java
@Test
void aRunNeverTakesLessThanNothing() {
var second = Duration.ofSeconds(1);
var ticks = durations().atLeast(second.negated()).atMost(second);
forAll(lists(ticks).ofSize(atMost(10)))
.check(
readings -> {
var took = elapsed(readings);
assertThat(took).isGreaterThanOrEqualTo(Duration.ZERO);
});
}
Terminal window
Expecting actual:
-1S
to be greater than or equal to:
0S
aRunNeverTakesLessThanNothing failed after 1 try (seed 0x5eed)
shrunk counterexample: readings = [PT-1S]
pin it: .example(List.of(Duration.parse("PT-1S")))
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.aRunNeverTakesLessThanNothing'

One reading a second back, the bound, is enough: the code has to decide what a run that took less than nothing means.

Twice a year most of the world’s clocks jump, and code that adds hours on the wall clock is wrong for the hour around each jump: a reminder an hour after a meeting can come two hours later, or none. offsets() never jumps, as a fixed offset has no summer time. zones() draws region zones, London, New York, Lord Howe’s half-hour summer time and others, and in .inZones(zones()) about one try in five lands a second, an hour or a day either side of one of the zone’s transitions:

DatesTest.java
/** The reminder for a meeting: an hour after it starts, on the wall clock. */
static ZonedDateTime anHourLater(ZonedDateTime start) {
var wall = start.toLocalDateTime().plusHours(1);
return wall.atZone(start.getZone());
}
DatesTest.java
@Test
void aReminderComesAnHourAfterTheStart() {
forAll(dateTimes().inZones(zones()))
.check(
start -> {
var waited = Duration.between(start, anHourLater(start));
assertThat(waited).isEqualTo(Duration.ofHours(1));
});
}
Terminal window
expected: 1H
but was: 2H
aReminderComesAnHourAfterTheStart failed after 59 tries (seed 0x5eed)
shrunk counterexample:
start = 2000-10-29T01:00+01:00[Europe/London]
tzdb:
2026e
pin it:
.example(ZonedDateTime.of(LocalDateTime.of(2000, 10, 29, 1, 0), ZoneId.of("Europe/London")))
rerun:
PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.aReminderComesAnHourAfterTheStart'

It shrank to the simplest start that fails: the clocks went back at 02:00 on 29 October 2000 in London, so an hour on the wall clock from 01:00 was two by the watch. .plusHours(1) on the ZonedDateTime itself counts real hours.

tzdb is the version of the time zone database the zone’s rules came from. Each JDK carries its own copy, which an update replaces, so a counterexample saved under one version and replayed under another may not be the same moment; the failure says so on a ! line when that happens.

Code that imports or exports spreadsheet cells meets serial dates: a date as a number of days. Excel’s default system counts from 1 January 1900, serial 1, and treats 1900 as a leap year, as Lotus 1-2-3 did, so its serial 60 is a 29 February 1900 that never was. Its other system, the old Mac default, counts from 1 January 1904, 1,462 days later. OLE Automation’s, which .NET’s DateTime.ToOADate writes, counts from 30 December 1899, agrees with Excel’s from 1 March 1900, and goes negative before it.

.inSerials(…) draws dates as serials in any of the three, Serial.EXCEL_1900, Serial.EXCEL_1904 or Serial.OLE_AUTOMATION, from io.proptastic.serials. It adds the serials no date gives, 0 and the fictitious 60 first. An importer that forgot the leap day that wasn’t:

Cells.java
private static final LocalDate MARCH_1900 = LocalDate.of(1900, 3, 1);
/** The date of a 1900-system cell: Excel's serial 1 is 1 January 1900. */
static LocalDate importDate(double serial) {
var day = (long) Math.floor(serial);
return day < 61
? LocalDate.of(1899, 12, 31).plusDays(day)
: LocalDate.of(1899, 12, 30).plusDays(day);
}
/** A date's 1900-system serial, a day lower before March 1900. */
static double exportDate(LocalDate day) {
var zero = LocalDate.of(1899, 12, 30);
if (day.isBefore(MARCH_1900)) {
zero = zero.plusDays(1);
}
return ChronoUnit.DAYS.between(zero, day);
}
DatesTest.java
@Test
void anImportedCellExportsAsItWas() {
forAll(dates().inSerials(Serial.EXCEL_1900))
.tries(1_000)
.check(
serial -> {
var day = Cells.importDate(serial);
assertEquals(serial, Cells.exportDate(day));
});
}
Terminal window
expected: <60.0> but was: <61.0>
anImportedCellExportsAsItWas failed after 75 tries (seed 0x5eed)
shrunk counterexample: serial = 60.0
pin it: .example(60.0)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.anImportedCellExportsAsItWas'

Serial 60 imports as 1 March 1900, which exports as 61. The importer has to refuse 60, or say what it maps it to. The serials no date gives are one class of edges among the calendar’s, so 60 comes up about one try in two hundred: this property runs 1,000 tries, and met it after 75. A serial is a mapped date, so it shrinks with the date, to 36892.0 for 1 January 2001; a failure only a serial edge causes shrinks to that edge, here 60.

For the time of day, a serial’s fraction: draw date-times on a millisecond grid, convert each to a serial and back in both systems, and expect the start:

DatesTest.java
static Domain<LocalDateTime> toTheMillisecond() {
var first = LocalDateTime.of(1904, 1, 1, 0, 0);
return dateTimes().atLeast(first).every(Duration.ofMillis(1));
}
@Test
void aCellsDateRoundTrips() {
forAll(toTheMillisecond(), elements(DateSystem.values()))
.check(
(time, system) -> {
var serial = Cells.toSerial(time, system);
assertEquals(time, Cells.fromSerial(serial, system));
});
}

It passes: a double near today’s serials holds about 0.6 µs, so milliseconds survive. Take the grid away and it fails, shrinking to the anchor with one stray nanosecond:

DatesTest.java
@Test
void aCellsDateRoundTripsToTheNanosecond() {
forAll(dateTimes().atLeast(LocalDateTime.of(1904, 1, 1, 0, 0)))
.check(
time -> {
var serial = Cells.toSerial(time, EXCEL_1900);
assertEquals(time, Cells.fromSerial(serial, EXCEL_1900));
});
}
Terminal window
expected: <2001-01-01T00:00:00.000000001> but was: <2001-01-01T00:00>
aCellsDateRoundTripsToTheNanosecond failed after 50 tries (seed 0x5eed)
shrunk counterexample: time = 2001-01-01T00:00:00.000000001
pin it: .example(LocalDateTime.of(2001, 1, 1, 0, 0, 0, 1))
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'DatesTest.aCellsDateRoundTripsToTheNanosecond'
  • A type from another library, such as Joda-Time’s dates or Joda-Money’s amounts: Domains maps proptastic’s own.
  • Domains measures how often each date domain draws what.
  • Every value: .exhaustively() runs each day of a short range once.

Privacy