Class DateTimeUtil

java.lang.Object
net.okapia.osid.torrefacto.util.DateTimeUtil

public final class DateTimeUtil extends Object
Quick conversions from an OSID DateTime to the common Java date and time classes. Only the org.osid.calendaring.DateTime interface is used, so any provider's implementation may be passed. The Java classes are Gregorian and tied to a single clock, so only a datetime on the Gregorian calendar in UTC time can be converted; any other calendar or time system is rejected. Components finer than the datetime's granularity are undefined and are floored (an undefined month or day becomes 1, an undefined time component becomes 0). If the granularity is coarser than a year the year itself is undefined and the datetime cannot be converted. When a value does not fit the target type, an InvalidArgumentException is thrown rather than silently wrapping or truncating the date. Sub-nanosecond precision, where present, is dropped.
  • Method Details

    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(DateTime datetime)
      Converts an OSID DateTime to a LocalDateTime. The value is read as a proleptic Gregorian date in UTC.
      Parameters:
      datetime - an OSID datetime
      Returns:
      the equivalent local datetime
      Throws:
      InvalidArgumentException - datetime is not Gregorian/UTC, has no defined year, or is out of bounds for a LocalDateTime
      NullArgumentException - datetime is null
    • toOffsetDateTime

      public static OffsetDateTime toOffsetDateTime(DateTime datetime)
      Converts an OSID DateTime to an OffsetDateTime. The offset is always UTC, as a convertible datetime is in UTC.
      Parameters:
      datetime - an OSID datetime
      Returns:
      the equivalent UTC offset datetime
      Throws:
      InvalidArgumentException - datetime is not Gregorian/UTC, has no defined year, or is out of bounds for an OffsetDateTime
      NullArgumentException - datetime is null
    • toDate

      public static Date toDate(DateTime datetime)
      Converts an OSID DateTime to a java.util.Date. Sub-millisecond precision is dropped.
      Parameters:
      datetime - an OSID datetime
      Returns:
      the equivalent date
      Throws:
      InvalidArgumentException - datetime is not Gregorian/UTC, has no defined year, or is out of bounds for a Date
      NullArgumentException - datetime is null