Class TimeUtils

java.lang.Object
de.iip_ecosphere.platform.support.TimeUtils

public class TimeUtils extends Object
Test utilities.
Author:
Holger Eichelberger, SSE
  • Field Details

  • Constructor Details

    • TimeUtils

      private TimeUtils()
      Preventing external creation.
  • Method Details

    • registerConverter

      public static void registerConverter(TimeUtils.DateConverter<?> converter)
      Registers an additional converter.
      Parameters:
      converter - the converter
    • registerFormat

      public static void registerFormat(String name, String pattern)
      Registers a default format for SimpleDateFormat.
      Parameters:
      name - the symbolic name of the format
      pattern - the pattern format to apply
      Throws:
      IllegalArgumentException - if pattern is illegal
    • format

      public static String format(Date data, String format) throws IOException
      Formats a date object to a given format.
      Parameters:
      data - the data/date
      format - the format, may be from SimpleDateFormat or ISO8601_FORMAT
      Returns:
      if no format can be constructed
      Throws:
      IOException - if formatting is not possible or the format is unknown
    • convertToDate

      private static <T> Date convertToDate(Class<T> cls, Object data)
      Uses one of the registered data converters to convert data.
      Type Parameters:
      T - the type of data
      Parameters:
      cls - the type of data
      data - the data (must not be null)
      Returns:
      the converted instance, may be null if there is no conversion
    • parse

      public static Date parse(Object data, String format) throws IOException
      Parses a date from the given data for the specified format.
      Parameters:
      data - the data (may be null, the the result will be null)
      format - the format may be from SimpleDateFormat or ISO8601_FORMAT
      Returns:
      the parsed date
      Throws:
      IOException - if parsing is not possible or the format is unknown
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(String format) throws IOException
      Returns a (cached) simple date formatter.
      Parameters:
      format - the format for the requested formatter for a string for SimpleDateFormat
      Returns:
      the formatter instance
      Throws:
      IOException - if format is not valid
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Date date)
      Converts a Date value to LocalDateTime using instant and the system default time zone.
      Parameters:
      date - the date to convert (may be null)
      Returns:
      the converted date (may be null)
    • toDate

      public static Date toDate(LocalDateTime date)
      Converts a LocalDateTime value to Date using instant and the system default time zone.
      Parameters:
      date - the date to convert (may be null)
      Returns:
      the converted date (may be null)
    • sleep

      public static void sleep(int ms)
      Just sleeps for the given amount of milliseconds.
      Parameters:
      ms - the milliseconds to wait for
    • waitFor

      public static boolean waitFor(Supplier<Boolean> continueCondition, int timeoutMs, int sleepMs)
      A generic function that waits either until the timeout is reached or the endCondition returns true.
      Parameters:
      continueCondition - function that returns true when waiting go on and false when waiting shall stop
      timeoutMs - timeout from calling in ms, may be negative for endless waiting
      sleepMs - time between two trials of timeoutMs and endCondition
      Returns:
      true if the continueCondition stopped waiting, false if the timeout caused the end of waiting