Package net.datafaker

Class DateAndTime

java.lang.Object
net.datafaker.DateAndTime

public class DateAndTime extends Object
A generator of random dates.
Since:
0.8.0
Author:
pmiklos
  • Constructor Details

    • DateAndTime

      protected DateAndTime(Faker faker)
  • Method Details

    • future

      public Timestamp future(int atMost, TimeUnit unit)
      Generates a future date from now. Note that there is a 1 second slack to avoid generating a past date.
      Parameters:
      atMost - at most this amount of time ahead from now exclusive.
      unit - the time unit.
      Returns:
      a future date from now.
    • future

      public String future(int atMost, TimeUnit unit, String pattern)
      Generates and converts to string representation a future date from now. Note that there is a 1 second slack to avoid generating a past date.
      Parameters:
      atMost - at most this amount of time ahead from now exclusive.
      unit - the time unit.
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a future date from now.
    • future

      public Timestamp future(int atMost, int minimum, TimeUnit unit)
      Generates a future date from now, with a minimum time.
      Parameters:
      atMost - at most this amount of time ahead from now exclusive.
      minimum - the minimum amount of time in the future from now.
      unit - the time unit.
      Returns:
      a future date from now, with a minimum time.
    • future

      public String future(int atMost, int minimum, TimeUnit unit, String pattern)
      Generates and converts to string representation of a future date from now, with a minimum time.
      Parameters:
      atMost - at most this amount of time ahead from now exclusive.
      minimum - the minimum amount of time in the future from now.
      unit - the time unit.
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a future date from now, with a minimum time.
    • future

      public Timestamp future(int atMost, TimeUnit unit, Date referenceDate)
      Generates a future date relative to the referenceDate.
      Parameters:
      atMost - at most this amount of time ahead to the referenceDate exclusive.
      unit - the time unit.
      referenceDate - the future date relative to this date.
      Returns:
      a future date relative to referenceDate.
    • future

      public String future(int atMost, TimeUnit unit, Date referenceDate, String pattern)
      Generates and converts to string representation a future date relative to the referenceDate.
      Parameters:
      atMost - at most this amount of time ahead to the referenceDate exclusive.
      unit - the time unit.
      referenceDate - the future date relative to this date.
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a future date relative to referenceDate.
    • past

      public Timestamp past(int atMost, TimeUnit unit)
      Generates a past date from now. Note that there is a 1 second slack added.
      Parameters:
      atMost - at most this amount of time earlier from now exclusive.
      unit - the time unit.
      Returns:
      a past date from now.
    • past

      public String past(int atMost, TimeUnit unit, String pattern)
      Generates a string representation of a past date from now. Note that there is a 1 second slack added.
      Parameters:
      atMost - at most this amount of time earlier from now exclusive.
      unit - the time unit.
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a past date from now.
    • past

      public Timestamp past(int atMost, int minimum, TimeUnit unit)
      Generates a past date from now, with a minimum time.
      Parameters:
      atMost - at most this amount of time earlier from now exclusive.
      minimum - the minimum amount of time in the past from now.
      unit - the time unit.
      Returns:
      a past date from now.
    • past

      public String past(int atMost, int minimum, TimeUnit unit, String pattern)
      Generates and converts to string representation a past date from now, with a minimum time.
      Parameters:
      atMost - at most this amount of time earlier from now exclusive.
      minimum - the minimum amount of time in the past from now.
      unit - the time unit.
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a past date from now, with a minimum time.
    • past

      public Timestamp past(int atMost, TimeUnit unit, Date referenceDate)
      Generates a past date relative to the referenceDate.
      Parameters:
      atMost - at most this amount of time past to the referenceDate exclusive.
      unit - the time unit.
      referenceDate - the past date relative to this date.
      Returns:
      a past date relative to referenceDate.
    • past

      public String past(int atMost, TimeUnit unit, Date referenceDate, String pattern)
      Generates a string representation of a past date relative to the referenceDate.
      Parameters:
      atMost - at most this amount of time past to the referenceDate exclusive.
      unit - the time unit.
      referenceDate - the past date relative to this date.
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a past date relative to referenceDate.
    • between

      public Timestamp between(Timestamp from, Timestamp to) throws IllegalArgumentException
      Generates a random date between two dates.
      Parameters:
      from - the lower bound inclusive
      to - the upper bound exclusive
      Returns:
      a random date between from and to.
      Throws:
      IllegalArgumentException - if the to date represents an earlier date than from date.
    • between

      public String between(Timestamp from, Timestamp to, String pattern) throws IllegalArgumentException
      Generates a string representation of a random date between two dates.
      Parameters:
      from - the lower bound inclusive
      to - the upper bound exclusive
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a random date between from and to.
      Throws:
      IllegalArgumentException - if the to date represents an earlier date than from date.
    • birthday

      public Timestamp birthday()
      Generates a random birthday between 65 and 18 years ago from now.
      Returns:
      a random birthday between 65 and 18 years ago from now.
    • birthday

      public String birthday(String pattern)
      Generates a string representation of a random birthday between 65 and 18 years ago from now.
      Parameters:
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a random birthday between 65 and 18 years ago from now.
    • birthday

      public Timestamp birthday(int minAge, int maxAge)
      Generates a random birthday between two ages from now.
      Parameters:
      minAge - the minimal age
      maxAge - the maximal age
      Returns:
      a random birthday between minAge and maxAge years ago from now.
      Throws:
      IllegalArgumentException - if the maxAge is lower than minAge.
    • birthday

      public String birthday(int minAge, int maxAge, String pattern)
      Generates and converts to string representation a random birthday between two ages from now.
      Parameters:
      minAge - the minimal age
      maxAge - the maximal age
      pattern - date time pattern to convert to string.
      Returns:
      a string representation of a random birthday between minAge and maxAge years ago from now.
      Throws:
      IllegalArgumentException - if the maxAge is lower than minAge.
    • duration

      public Duration duration(long max, ChronoUnit unit)
      Generates a random Duration lower than max.
      Parameters:
      max - the maximum value
      unit - the temporal unit (day or shorter than a day)
      Returns:
      a random Duration lower than max.
      Throws:
      IllegalArgumentException - if the unit is invalid.
    • duration

      public Duration duration(long min, long max, ChronoUnit unit)
      Generates a random Duration between min and max.
      Parameters:
      min - the maximum value
      max - the minimal value
      unit - the temporal unit (day or shorter than a day)
      Returns:
      a random Duration between min inclusive and max exclusive if max greater min.
      Throws:
      IllegalArgumentException - if the unit is invalid.
    • duration

      public Duration duration(long max, String unit)
      Generates a random Duration lower than max.
      Parameters:
      max - the maximum value
      unit - the temporal unit (day or shorter than a day)
      Returns:
      a random Duration lower than max.
      Throws:
      IllegalArgumentException - if the unit is invalid.
    • duration

      public Duration duration(long min, long max, String unit)
      Generates a random Duration between min and max.
      Parameters:
      min - the minimal value
      max - the maximum value
      unit - the temporal unit (day or shorter than a day)
      Returns:
      a random Duration between min inclusive and max exclusive if max greater min.
      Throws:
      IllegalArgumentException - if the unit is invalid.
    • period

      public Period period(Period min, Period max)
      Generates a random Period between min and max.
      Parameters:
      min - the minimal value
      max - the maximum value
      Returns:
      a random Period between min inclusive and max inclusive if max greater min.
      Throws:
      IllegalArgumentException - if the min is greater than max.