/** * 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 random birthday between two ages. * * @param minAge * the minimal age * @param maxAge * the maximal age * @return a random birthday between {@code minAge} and {@code maxAge} years ago. * @throws IllegalArgumentException * if the {@code maxAge} is lower than {@code minAge}. */ public Date birthday(int minAge, int maxAge) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); Calendar from = new GregorianCalendar(currentYear - maxAge, 0, 1); Calendar to = new GregorianCalendar(currentYear - minAge, 11, 31); return between(from.getTime(), to.getTime()); }
/** * Generates a random birthday between 65 and 18 years ago. * * @return a random birthday between 65 and 18 years ago. */ public Date birthday() { return birthday(DEFAULT_MIN_AGE, DEFAULT_MAX_AGE); }
/** * Generates a past date from now. Note that there is a 1 second slack added. * * @param atMost * at most this amount of time earlier from now exclusive. * @param unit * the time unit. * @return a past date from now. */ public Date past(int atMost, TimeUnit unit) { Date now = new Date(); Date aBitEarlierThanNow = new Date(now.getTime() - 1000); return past(atMost, unit, aBitEarlierThanNow); }
this.food = new Food(this); this.gameOfThrones = new GameOfThrones(this); this.dateAndTime = new DateAndTime(this); this.demographic = new Demographic(this); this.dog = new Dog(this);
/** * Generates a past date from now, with a minimum time. * * @param atMost * at most this amount of time earlier from now exclusive. * @param minimum * the minimum amount of time in the past from now. * @param unit * the time unit. * @return a past date from now. */ public Date past(int atMost, int minimum, TimeUnit unit) { Date now = new Date(); Date minimumDate = new Date(now.getTime() - unit.toMillis(minimum)); return past(atMost - minimum, unit, minimumDate); }
/** * 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()); }