Class RelativeDateTimeParser

java.lang.Object
org.dbunit.util.RelativeDateTimeParser

public class RelativeDateTimeParser extends Object

A parser for relative date time string.
The basic format is [now{diff...}{time}].
'diff' consists of two parts 1) a number with a leading plus or minus sign and 2) a character represents temporal unit. See the table below for the supported units. There can be multiple 'diff's and they can be specified in any order.
'time' is a string that can be parsed by LocalTime#parse(). If specified, it is used instead of the current time.
Both 'diff' and 'time' are optional.
Whitespaces are allowed before and after each 'diff'.

Unit

  • y : years
  • M : months
  • d : days
  • h : hours
  • m : minutes
  • s : seconds

Here are some examples.

  • [now] : current date time.
  • [now-1d] : the same time yesterday.
  • [now+1y+1M-2h] : a year and a month from today, two hours earlier.
  • [now+1d 10:00] : 10 o'clock tomorrow.
  • Constructor Details

    • RelativeDateTimeParser

      public RelativeDateTimeParser()
      Default constructor.
    • RelativeDateTimeParser

      public RelativeDateTimeParser(Clock clock)
      Constructs a parser resolving [now] relative to the given clock.
      Parameters:
      clock - the clock used to resolve [now].
  • Method Details

    • parse

      public LocalDateTime parse(String input)
      Parses a relative datetime expression such as [now-1d].
      Parameters:
      input - the relative datetime expression to parse.
      Returns:
      the resolved date and time.
    • getClock

      public Clock getClock()
      Returns the clock used to resolve [now].
      Returns:
      the clock used to resolve [now].
    • setClock

      public void setClock(Clock clock)
      Sets the clock used to resolve [now].
      Parameters:
      clock - the clock used to resolve [now].