Class DateTimeHelper

java.lang.Object
org.n52.shetland.util.DateTimeHelper

public final class DateTimeHelper extends Object
Utility class for Time formatting and parsing. Uses Joda Time.
Since:
1.0.0
  • Method Details

    • parseIsoString2DateTime

      public static org.joda.time.DateTime parseIsoString2DateTime(String timeString) throws DateTimeParseException
      Parses a time String to a Joda Time DateTime object
      Parameters:
      timeString - Time String
      Returns:
      DateTime object
      Throws:
      DateTimeParseException - If an error occurs.
    • parseString2DateTime

      public static org.joda.time.DateTime parseString2DateTime(String timeString, String dateFormat) throws DateTimeParseException
      Throws:
      DateTimeParseException
    • parseIsoString2DateTime2Time

      public static Time parseIsoString2DateTime2Time(String timeString) throws DateTimeParseException
      Parses the given ISO 8601 String to a Time including TimeInstant and TimePeriod
      Parameters:
      timeString - a ISO 8601 formatted time string
      Returns:
      a Time object
      Throws:
      DateTimeParseException - If an error occurs.
    • format

      public static String format(Time time)
      Formats the given Time to ISO 8601 string.
      Parameters:
      time - an Time object to be formatted
      Returns:
      an ISO 8601 conform String.
      Throws:
      IllegalArgumentException - in the case of receiving null or not supported types.
      See Also:
    • formatDateTime2IsoString

      public static String formatDateTime2IsoString(org.joda.time.DateTime dateTime)
      Formats a DateTime to a ISO-8601 String
      Parameters:
      dateTime - Time object
      Returns:
      ISO-8601 formatted time String
    • formatDateTime2ResponseString

      public static String formatDateTime2ResponseString(org.joda.time.DateTime dateTime) throws DateTimeFormatException
      Formats a DateTime to a String using the response format
      Parameters:
      dateTime - Time object
      Returns:
      Response formatted time String
      Throws:
      DateTimeFormatException - If an error occurs.
    • formatDateTime2String

      public static String formatDateTime2String(org.joda.time.DateTime dateTime, Time.TimeFormat timeFormat) throws DateTimeFormatException
      Get formatted string from DateTime as defined Time.TimeFormat
      Parameters:
      dateTime - DateTime to format
      timeFormat - The response Time.TimeFormat
      Returns:
      The formatted DateTime
      Throws:
      DateTimeFormatException - If an error occurs when formatting the DateTime
    • formatDateTime2String

      public static String formatDateTime2String(TimePosition timePosition) throws DateTimeFormatException
      Get formatted string from TimePosition
      Parameters:
      timePosition - TimePosition to format
      Returns:
      The formatted TimePosition
      Throws:
      DateTimeFormatException - If an error occurs when formatting the TimePosition
    • formatDateTime2FormattedString

      public static String formatDateTime2FormattedString(org.joda.time.DateTime dateTime, String dateFormat) throws DateTimeFormatException
      Formats a DateTime to a String using specified format
      Parameters:
      dateTime - Time object
      dateFormat - the date time format
      Returns:
      Specified formatted time String
      Throws:
      DateTimeFormatException - If an error occurs.
    • formatDateTime2FormattedString

      public static String formatDateTime2FormattedString(org.joda.time.DateTime dateTime, org.joda.time.format.DateTimeFormatter formatter) throws DateTimeFormatException
      Formats a DateTime to a DAte using specified DateTimeFormatter
      Parameters:
      dateTime - Time object
      formatter - the DateTimeFormatter
      Returns:
      Specified formatted time String
      Throws:
      DateTimeFormatException - If an error occurs.
    • formatDateTime2YearMonthDayDateStringYMD

      public static String formatDateTime2YearMonthDayDateStringYMD(org.joda.time.DateTime dateTime)
      formats a DateTime to a string with year-month-day.
      Parameters:
      dateTime - The DateTime.
      Returns:
      Returns formatted time String.
      Throws:
      DateTimeFormatException - If an error occurs.
    • formatDateTime2YearMonthDateString

      public static String formatDateTime2YearMonthDateString(org.joda.time.DateTime dateTime) throws DateTimeFormatException
      formats a DateTime to a string with year-month.
      Parameters:
      dateTime - The DateTime.
      Returns:
      Returns formatted time String.
      Throws:
      DateTimeFormatException - If an error occurs.
    • formatDateTime2YearDateString

      public static String formatDateTime2YearDateString(org.joda.time.DateTime dateTime) throws DateTimeFormatException
      formats a DateTime to a string with year.
      Parameters:
      dateTime - The DateTime.
      Returns:
      Returns formatted time String.
      Throws:
      DateTimeFormatException - If an error occurs.
    • getTimeLengthBeforeTimeZone

      public static int getTimeLengthBeforeTimeZone(String time)
    • setDateTime2EndOfMostPreciseUnit4RequestedEndPosition

      public static org.joda.time.DateTime setDateTime2EndOfMostPreciseUnit4RequestedEndPosition(org.joda.time.DateTime dateTime, int isoTimeLength)
      Set the time object to the end values (seconds, minutes, hours, days,..) if the time Object has not all values
      Parameters:
      dateTime - Time object
      isoTimeLength - Length of the time object
      Returns:
      Modified time object.
    • setDateTime2EndOfMostPreciseUnit4RequestedEndPosition

      public static org.joda.time.DateTime setDateTime2EndOfMostPreciseUnit4RequestedEndPosition(String time) throws DateTimeParseException
      Throws:
      DateTimeParseException
    • parseDuration

      public static org.joda.time.Period parseDuration(String duration)
      Parse a duration from a String representation
      Parameters:
      duration - Duration as String
      Returns:
      Period object of duration
    • calculateExpiresDateTime

      public static org.joda.time.DateTime calculateExpiresDateTime(org.joda.time.DateTime start)
      Calculates the expire time for a time object
      Parameters:
      start - Time object
      Returns:
      Expire time
    • setResponseFormat

      public static void setResponseFormat(String responseFormat)
      Set the response format
      Parameters:
      responseFormat - Defined response format
    • setLease

      public static void setLease(int lease)
      Set the lease value
      Parameters:
      lease - Defined lease value
    • makeDateTime

      public static org.joda.time.DateTime makeDateTime(Object object)
      Make a new UTC DateTime from an object
      Parameters:
      object - Object to make DateTime
      Returns:
      DateTime, or null if object was null
    • toUTC

      public static org.joda.time.DateTime toUTC(org.joda.time.DateTime datetime)
      Transforms the supplied DateTime to UTC.
      Parameters:
      datetime - the date time (may be null)
      Returns:
      the UTC time (or null
    • max

      public static org.joda.time.DateTime max(org.joda.time.DateTime dt1, org.joda.time.DateTime dt2)
      Find the max of two dates (null safe)
      Parameters:
      dt1 - first DateTime
      dt2 - second DateTime
      Returns:
      Max of two dates
    • getDaysSince

      public static int getDaysSince(org.joda.time.DateTime start, org.joda.time.DateTime end)
      Get days between the given DateTimes
      Parameters:
      start - Start DateTime
      end - End DateTime
      Returns:
      Days between the two DateTimes
    • getDaysSinceWithPrecision

      public static double getDaysSinceWithPrecision(org.joda.time.DateTime start, org.joda.time.DateTime end)
      Get days between the given DateTimes with precision
      Parameters:
      start - Start DateTime
      end - End DateTime
      Returns:
      Days with precisions between the two DateTimes
    • getMinutesSince

      public static int getMinutesSince(org.joda.time.DateTime start, org.joda.time.DateTime end)
      Get minutes between the given DateTimes
      Parameters:
      start - Start DateTime
      end - End DateTime
      Returns:
      Minutes between the two DateTimes
    • getSecondsSinceEpoch

      public static double getSecondsSinceEpoch(org.joda.time.DateTime time)
      Get seconds since epoch
      Parameters:
      time - DateTime to get seconds for
      Returns:
      Seconds since epoch