/** * Generates a future date from now. Note that there is a 1 second slack to avoid generating a past date. * * @param atMost * at most this amount of time ahead from now exclusive. * @param unit * the time unit. * @return a future date from now. */ public Date future(int atMost, TimeUnit unit) { Date now = new Date(); Date aBitLaterThanNow = new Date(now.getTime() + 1000); return future(atMost, unit, aBitLaterThanNow); }
/** * Generates a future date from now, with a minimum time. * * @param atMost * at most this amount of time ahead from now exclusive. * @param minimum * the minimum amount of time in the future from now. * @param unit * the time unit. * @return a future date from now. */ public Date future(int atMost, int minimum, TimeUnit unit) { Date now = new Date(); Date minimumDate = new Date(now.getTime() + unit.toMillis(minimum)); return future(atMost - minimum, unit, minimumDate); }
protected DataProvider collectDateProperty(Swagger swagger, Property schema, boolean clean) { if (schema instanceof DateTimeProperty) return gen(() -> { Date dateTime = faker.date().future((int) faker.random().nextDouble(), TimeUnit.DAYS); return LocalDateTime.ofInstant(dateTime.toInstant(), ZoneId.of("UTC")); }); else if (schema instanceof DateProperty) return gen(() -> { Date dateTime = faker.date().future((int) faker.random().nextDouble(), TimeUnit.DAYS); return LocalDateTime.ofInstant(dateTime.toInstant(), ZoneId.of("UTC")).toLocalDate(); }); throw new IllegalArgumentException("Unsupported property type: " + schema.getClass()); }