Class DateTimeValue

java.lang.Object
ch.tocco.nice2.optional.calendar.api.util.DateTimeValue
All Implemented Interfaces:
Serializable, Comparable<DateTimeValue>

public class DateTimeValue extends Object implements Serializable, Comparable<DateTimeValue>

This class represents a date or date-time value, with an optionally attached time zone.

If no time zone is attached, it is treated as a local date/datet-time value, but the internal value is normalized to UTC time zone. If only a date value is given (no time information), the time is set to midnight.

For any operation that needs a time zone, if none is specified, the UTC time zone is used. Check with isLocal() whether a time zone is specified or not.

This class is supposed to ease creation of a UTC timestamp values. Internally the point in time is maintained as a UNIX timestamp (milliseconds offset from 1970-01-01T00:00:00Z).

This class is immutable.

See Also:
  • Constructor Details

    • DateTimeValue

      public DateTimeValue(long millis, @Nullable @Nullable org.joda.time.DateTimeZone timezone, boolean datetime)
      Ctor.
      Parameters:
      millis - the millis as in UNIX timestamp
      timezone - the time zone (may be null to specifiy a local date)
      datetime - if this is a date-only (false) or datetime (true) value
  • Method Details

    • dateTime

      public static DateTimeValue dateTime(org.joda.time.ReadableInstant instant)
      Creates a date-time value with the information supplied by the instant. This will set the millis from the instant as well as the time zone.
    • dateTime

      public static DateTimeValue dateTime(org.joda.time.LocalDateTime datetime)
      Creates a date-time value from the specified local-time. This will set the milliseconds for the local date-time in time zone UTC. But no time zone is set into the returned DateTimeValue.
    • date

      public static DateTimeValue date(org.joda.time.LocalDate date)
      Creates a local date value.
    • date

      public static DateTimeValue date(org.joda.time.ReadableInstant datetime)
    • parse

      public static DateTimeValue parse(String value)
    • withZone

      public DateTimeValue withZone(org.joda.time.DateTimeZone tz)
      Returns a new DateTimeValue with the same millis and the specified time zone.
    • isDate

      public boolean isDate()
      Returns whether this represents a date-only value - no time information is specified.
    • isDatetime

      public boolean isDatetime()
      Returns whether this represents a date-time value.
    • isLocal

      public boolean isLocal()
      Returns whether this is a local date/datetime value - no time zone is specified.
    • getTimezone

      @Nullable public @Nullable org.joda.time.DateTimeZone getTimezone()
      Returns the time zone. Maybe null if this is a local datetime value.
    • getMillis

      public long getMillis()
      Returns the millis of this date-time value.
    • toLocalDateTimeForUTC

      public DateTimeValue toLocalDateTimeForUTC()
      Uses this local date time value (ex. 2005-10-22T15:40, discarding any time zone info) and carries it into the UTC time zone. The milliseconds are adopted to represent the new point in time.
    • toDateTime

      public org.joda.time.DateTime toDateTime(org.joda.time.DateTimeZone zone)

      Returns the datetime representation of this date-time value.

      If this is a local/floating date-time value, the DateTime with the specified time zone is returned. The point in time is calculated to match the same local time in the specified time zone.

    • toDateTime

      public org.joda.time.DateTime toDateTime()
      This is a shortcut for #toDateTime(DateTimeZone.getDefault()).
      Returns:
      joda's DateTime for this
      See Also:
    • getUTCDateTime

      public org.joda.time.DateTime getUTCDateTime()
      Returns this value as DateTime with UTC time zone.
    • getDateTimeAtStartOfDay

      public org.joda.time.DateTime getDateTimeAtStartOfDay()
      Returns a DateTime object whose millis are set to the start of this day. If isLocal() is true the UTC time zone is used.
    • getDateTimeAtEndOfDay

      public org.joda.time.DateTime getDateTimeAtEndOfDay()
      Returns a DateTime object whose millis are set to the end of this day. If isLocal() is true the UTC time zone is used.
    • isAfter

      public boolean isAfter(DateTimeValue dt)
      Returns whether this is after the specified value. Any date-only value the start of this day is used for comparing. It compares millis only.
    • isBefore

      public boolean isBefore(DateTimeValue dt)
      Returns whether this is before the specified value. Any date-only value the start of this day is used for comparing. It compares millis only.
    • isEqual

      public boolean isEqual(DateTimeValue dt)
      Returns whether this is equal the specified value. Any date-only value the start of this day is used for comparing. It compares millis only.
    • toDateTimeString

      public String toDateTimeString()

      Returns a string representation of the date or datetime in a format that is recognized by jodas ctors.

           yyyy-MM-ddTHH:mm
       

      the time information is optional.

      The string returned can be parsed by parse(String).

    • toBasicDateTimeString

      public String toBasicDateTimeString()
      Returns a string of the form
           yyyyMMddTHHmmss[Z]
       
      where time is optional. The string is converted to UTC time zone!
    • getWeekday

      public Weekday getWeekday()
      Returns the weekday of this date.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(DateTimeValue o)
      Specified by:
      compareTo in interface Comparable<DateTimeValue>
    • toString

      public String toString()
      Overrides:
      toString in class Object