Class GDateTime

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class GDateTime
    extends java.lang.Object
    implements java.lang.Cloneable
    The date and/or time property value is either a LocalTime, OffsetTime, LocalDate, LocalDateTime, OffsetDateTime or ZoneId and is used for the DateTimeProp property. The class is immutable and the clone returns the same instance.

    Note: GDateTime.Type.ZoneId does not have a corresponding direct representation in Value.

    For all conversions, the Locale Information is used to get the Zone ID or Zone Offset for any conversions that take place in the class. This information is retrieved either from the direct parameter of the method call, or by looking up this information from the current thread. If not found, the system default values are used.

    Several conversions methods in this class do not always need the Locale Information for a particular conversion, but the parameter may be present anyway. Setting it to null will cause it to look-up the value only when required.

    Author:
    Christopher Mindus
    • Nested Class Summary

      Nested Classes 
      Modifier and TypeClassDescription
      static class GDateTime.Type
      The type.
    • Field Summary

      Fields 
      Modifier and TypeFieldDescription
      static GDateTimeEPOCH_DATE
      The epoch date as a LocalDate, i.e.
      static GDateTimeMIDNIGHT
      The time as midnight as a LocalTime, i.e.
    • Constructor Summary

      Constructors 
      ConstructorDescription
      GDateTime​(java.time.LocalDate localDate)
      Constructs the date/time from a LocalDate.
      GDateTime​(java.time.LocalDateTime localDateTime)
      Constructs the date/time from a LocalDateTime.
      GDateTime​(java.time.LocalTime localTime)
      Constructs the date/time from a LocalTime.
      GDateTime​(java.time.OffsetDateTime offsetDateTime)
      Constructs the date/time from an OffsetDateTime.
      GDateTime​(java.time.OffsetTime offsetTime)
      Constructs the date/time from an OffsetTime.
      GDateTime​(java.time.ZonedDateTime zonedDateTime)
      Constructs the date/time from an OffsetDateTime.
      GDateTime​(java.time.ZoneId zone)
      Constructs the date/time from a ZoneId.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      static StringArrayPropcreateTimeZonesProp()
      Creates a new time zones array list property named Atom.ZONES.
      booleanequals​(java.lang.Object o)
      Checks if two date objects are equal.
      static GDateTimefromDate​(java.sql.Date date)
      Helper to constructs a date from an SQL Date instance as a LocalDate.
      static GDateTimefromDate​(java.util.Date date)
      Helper to constructs a date from a Date instance as a LocalDate.
      static GDateTimefromDateTime​(java.sql.Timestamp timeStamp)
      Helper to constructs a date/time from a timestamp as a LocalDateTime.
      static GDateTimefromDateTime​(java.util.Date dateTime)
      Helper to constructs a date/time from a data and time as a LocalDateTime.
      static GDateTimefromString​(GDateTime.Type type, java.lang.String string)
      Creates a new GDateTime value from a type and a String.
      static GDateTimefromTime​(java.sql.Time time)
      Helper to constructs a time from a Time instance as a LocalTime.
      static GDateTimefromTime​(java.util.Date time)
      Helper to constructs a time from a Date instance as a LocalTime.
      static GDateTimefromValue​(Value value)
      Helper to constructs a date/time from a Value, if the Value is of the Value Types LocalTime, OffsetTime, LocalDate, LocalDateTime, OffsetDateTime or ZonedDateTime.
      java.lang.StringgetGivenZoneName()
      Returns the zone name that given to the Zone ID or Zone Offset.
      java.time.LocalDategetLocalDate()
      Gets the value as a LocalDate.
      java.time.LocalDateTimegetLocalDateTime()
      Gets the value as a LocalDateTime.
      java.time.LocalTimegetLocalTime()
      Gets the value as a LocalTime.
      java.time.ZoneOffsetgetOffset()
      Gets the time offset value.
      java.time.OffsetDateTimegetOffsetDateTime()
      Gets the value as a OffsetDateTime.
      java.time.OffsetTimegetOffsetTime()
      Gets the value as a OffsetTime.
      java.lang.StringgetString()
      Gets the date/time as a String depending on the the instance type of, i.e.
      GDateTime.TypegetType()
      Returns the date/time type for this instance value.
      static GDateTime.TypegetType​(java.lang.Object value)
      Gets the type from an Object.
      java.time.temporal.TemporalgetValue()
      Gets the value object.
      java.lang.StringgetValueString()
      Gets the date/time value as a String, i.e.
      java.time.ZoneIdgetZone()
      Gets the time zone for the ZonedDateTime or ZoneId types, such as 'Europe/Paris'.
      java.time.ZonedDateTimegetZonedDateTime()
      Gets the value as a ZonedDateTime.
      booleanhasDate()
      Returns if the value consists of a date component.
      booleanhasTime()
      Returns if the value consists of a time component.
      booleanhasTimeOffset()
      Returns if the value has a time offset component.
      static intnanoOfSecond​(java.util.Date timeOrDateTime)
      Gets the nanos of seconds of a time value.
      static GDateTimenow​(GDateTime.Type type, LocaleInfo localeInfo)
      Create a new GDateTime with the current date and/or time for a type.
      protected java.lang.StringparamString()
      Returns the parameter string representing the state of this event.
      GDateTimetoDateValue()
      Gets the date/time value that would match a date value component.
      java.time.LocalDatetoLocalDate()
      Gets the date as a LocalDate with potential forced conversion.
      java.time.LocalDateTimetoLocalDateTime()
      Gets the date as a LocalDateTime with potential forced conversion.
      java.time.LocalTimetoLocalTime()
      Gets the current value as a LocalTime with potential forced conversion.
      java.time.OffsetDateTimetoOffsetDateTime​(LocaleInfo localeInfo)
      Gets the date as a OffsetDateTime with potential forced conversion.
      java.time.OffsetTimetoOffsetTime​(LocaleInfo localeInfo)
      Gets the current value as a OffsetTime with potential forced conversion.
      java.lang.StringtoString()
      Returns a string representation of this class instance and its values.
      GDateTimetoTimeValue()
      Gets the date/time value that would match a time value component.
      GDateTimetoType​(GDateTime.Type type, LocaleInfo localeInfo)
      Converts a date/time value to another type.
      java.time.ZonedDateTimetoZonedDateTime​(LocaleInfo localeInfo)
      Gets the date as a LocalDateTime with potential forced conversion, and will only work to convert an OffsetDateTime.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • EPOCH_DATE

        public static final GDateTime EPOCH_DATE
        The epoch date as a LocalDate, i.e. 1st of January 1970.
      • MIDNIGHT

        public static final GDateTime MIDNIGHT
        The time as midnight as a LocalTime, i.e. 00:00:00.
    • Constructor Detail

      • GDateTime

        public GDateTime​(java.time.LocalTime localTime)
        Constructs the date/time from a LocalTime.
        Parameters:
        localTime - The local time.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
      • GDateTime

        public GDateTime​(java.time.OffsetTime offsetTime)
        Constructs the date/time from an OffsetTime.
        Parameters:
        offsetTime - The OffsetTime.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
      • GDateTime

        public GDateTime​(java.time.LocalDate localDate)
        Constructs the date/time from a LocalDate.
        Parameters:
        localDate - The local date.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
      • GDateTime

        public GDateTime​(java.time.LocalDateTime localDateTime)
        Constructs the date/time from a LocalDateTime.
        Parameters:
        localDateTime - The local date and time.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
      • GDateTime

        public GDateTime​(java.time.OffsetDateTime offsetDateTime)
        Constructs the date/time from an OffsetDateTime.
        Parameters:
        offsetDateTime - The OffsetDateTime.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
      • GDateTime

        public GDateTime​(java.time.ZonedDateTime zonedDateTime)
        Constructs the date/time from an OffsetDateTime.
        Parameters:
        zonedDateTime - The ZonedDateTime.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
      • GDateTime

        public GDateTime​(java.time.ZoneId zone)
        Constructs the date/time from a ZoneId.
        Parameters:
        zone - The ZoneId.
        Throws:
        java.lang.NullPointerException - If the parameter is null.
    • Method Detail

      • createTimeZonesProp

        public static StringArrayProp createTimeZonesProp()
        Creates a new time zones array list property named Atom.ZONES. The time zones are sorted using the default locale.
        Returns:
        A new instance of the shared string array. The property is set to read-only.
      • getType

        public static GDateTime.Type getType​(java.lang.Object value)
        Gets the type from an Object. Only values of the classes LocalTime, OffsetTime, LocalDate, LocalDateTime, OffsetDateTime, ZonedDateTime and ZoneId return non-null.
        Returns:
        The Type that matches, or null for no match.
      • nanoOfSecond

        public static int nanoOfSecond​(java.util.Date timeOrDateTime)
        Gets the nanos of seconds of a time value.
      • fromTime

        public static GDateTime fromTime​(java.sql.Time time)
        Helper to constructs a time from a Time instance as a LocalTime.
        Returns:
        The new GDateTime instance with a LocalTime type.
      • fromTime

        public static GDateTime fromTime​(java.util.Date time)
        Helper to constructs a time from a Date instance as a LocalTime.
        Returns:
        The new GDateTime instance with a LocalTime type.
      • fromDate

        public static GDateTime fromDate​(java.sql.Date date)
        Helper to constructs a date from an SQL Date instance as a LocalDate.
        Returns:
        The new GDateTime instance with a LocalDate type.
      • fromDate

        public static GDateTime fromDate​(java.util.Date date)
        Helper to constructs a date from a Date instance as a LocalDate.
        Returns:
        The new GDateTime instance with a LocalDate type.
      • fromDateTime

        public static GDateTime fromDateTime​(java.sql.Timestamp timeStamp)
        Helper to constructs a date/time from a timestamp as a LocalDateTime.
        Returns:
        The new GDateTime instance with a LocalDateTime type.
      • fromDateTime

        public static GDateTime fromDateTime​(java.util.Date dateTime)
        Helper to constructs a date/time from a data and time as a LocalDateTime.
        Returns:
        The new GDateTime instance with a LocalDateTime type.
      • fromString

        public static GDateTime fromString​(GDateTime.Type type,
                                           java.lang.String string)
                                    throws java.time.format.DateTimeParseException
        Creates a new GDateTime value from a type and a String.
        Parameters:
        type - The type.
        string - The String.
        Returns:
        The new GDateTime instance.
        Throws:
        java.time.format.DateTimeParseException - If the string cannot be parsed.
      • now

        public static GDateTime now​(GDateTime.Type type,
                                    LocaleInfo localeInfo)
        Create a new GDateTime with the current date and/or time for a type. For ZoneId, the zone ID is retrieved from the locale information.
        Parameters:
        type - The type.
        Returns:
        The new GDateTime instance with the current date/time.
      • fromValue

        public static GDateTime fromValue​(Value value)
        Helper to constructs a date/time from a Value, if the Value is of the Value Types LocalTime, OffsetTime, LocalDate, LocalDateTime, OffsetDateTime or ZonedDateTime.

        Note: GDateTime.Type.ZoneId does not have a corresponding direct representation in Value.

        Parameters:
        value - The Value-value, null or null Value is NOT allowed.
        Returns:
        The new GDateTime instance with matching type, except ZoneId that does not have a corresponding Value type.
        Throws:
        java.lang.IllegalArgumentException - If the value is not of correct type, i.e. one of the Value Types LocalTime, OffsetTime, LocalDate, LocalDateTime, OffsetDateTime or ZonedDateTime, or the value is null.
      • hasDate

        public boolean hasDate()
        Returns if the value consists of a date component.
        Returns:
        true for LocalDate, LocalDateTime and OffsetDateTime, false for LocalTime and OffsetTime.
      • hasTime

        public boolean hasTime()
        Returns if the value consists of a time component.
        Returns:
        true for LocalDateTime and OffsetDateTime, LocalTime or OffsetTime, false for LocalDate.
      • hasTimeOffset

        public boolean hasTimeOffset()
        Returns if the value has a time offset component.
        Returns:
        true for OffsetTime and OffsetDateTime, false otherwise.
      • equals

        public boolean equals​(java.lang.Object o)
        Checks if two date objects are equal.
        Overrides:
        equals in class java.lang.Object
      • getType

        public GDateTime.Type getType()
        Returns the date/time type for this instance value.
      • getValue

        public java.time.temporal.Temporal getValue()
        Gets the value object.
        Returns:
        The value object as an instance of LocalTime, OffsetTime, LocalDate, LocalDateTime, OffsetDateTime, or ZonedDateTime. If the type is ZoneId, null is returned.
      • getString

        public java.lang.String getString()
        Gets the date/time as a String depending on the the instance type of, i.e. "type,dateTimeValue.toString()".
        Returns:
        the String as described above.
      • getValueString

        public java.lang.String getValueString()
        Gets the date/time value as a String, i.e. "dateTimeValue.toString()".
        Returns:
        the String as described above.
      • paramString

        protected java.lang.String paramString()
        Returns the parameter string representing the state of this event. This string is useful for debugging. Subclasses adds extra information to the string by preceding it with a comma followed by the extra information.
        Returns:
        the parameter string of this event.
      • toString

        public java.lang.String toString()
        Returns a string representation of this class instance and its values.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this class instance.
      • getLocalTime

        public java.time.LocalTime getLocalTime()
        Gets the value as a LocalTime.
        Returns:
        The time as LocalTime if the type is LocalTime, otherwise null.
      • getOffsetTime

        public java.time.OffsetTime getOffsetTime()
        Gets the value as a OffsetTime.
        Returns:
        The time as OffsetTime if the type is OffsetTime, otherwise null.
      • getLocalDate

        public java.time.LocalDate getLocalDate()
        Gets the value as a LocalDate.
        Returns:
        The date as LocalDate if the type is LocalDate, otherwise null.
      • getLocalDateTime

        public java.time.LocalDateTime getLocalDateTime()
        Gets the value as a LocalDateTime.
        Returns:
        The date and time as LocalDateTime if the type is LocalDateTime, otherwise null.
      • getOffsetDateTime

        public java.time.OffsetDateTime getOffsetDateTime()
        Gets the value as a OffsetDateTime.
        Returns:
        The date and time as OffsetDateTime if the type is OffsetDateTime, otherwise null.
      • getZonedDateTime

        public java.time.ZonedDateTime getZonedDateTime()
        Gets the value as a ZonedDateTime.
        Returns:
        The date and time as ZonedDateTime if the type is ZonedDateTime, otherwise null.
      • getOffset

        public java.time.ZoneOffset getOffset()
        Gets the time offset value.
        Returns:
        The zone offset for OffsetTime, OffsetDateTime and ZonedDateTime, null otherwise.
      • getZone

        public java.time.ZoneId getZone()
        Gets the time zone for the ZonedDateTime or ZoneId types, such as 'Europe/Paris'. It is also returned for OffsetTime and OffsetDateTime, but then it is a ZoneOffset (that is a subclass of ZoneId).

        This returns the zone ID. This identifies the time-zone rules that determine when and how the offset from UTC/Greenwich changes.

        Returns:
        The zone ID for OffsetTime, OffsetDateTime, ZonedDateTime and ZoneId, null otherwise.
      • getGivenZoneName

        public java.lang.String getGivenZoneName()
        Returns the zone name that given to the Zone ID or Zone Offset. This name may be different from what has been passed from the remote part in case it failed the ZoneId parsing.
        Returns:
        The zone name or ID for OffsetTime, OffsetDateTime, ZonedDateTime and ZoneId.
      • toDateValue

        public GDateTime toDateValue()
        Gets the date/time value that would match a date value component.
        Returns:
        A LocalDate type of value, i.e. this same instance for LocalDate, otherwise a new value for LocalDateTime, OffsetDateTime and ZonedDateTime with the type LocalDate.
        Throws:
        java.lang.IllegalStateException - If the type is LocalTime, OffsetTime or ZoneId.
      • toTimeValue

        public GDateTime toTimeValue()
        Gets the date/time value that would match a time value component. The time value can include a zone offset component in case the type is OffsetTime or OffsetDateTime.
        Returns:
        A LocalTime or OffsetTime type of value, i.e. this same instance for LocalTime, OffsetTime and ZonedDateTime, otherwise a new value for LocalDateTime and OffsetDateTime with the type LocalTime.
        Throws:
        java.lang.IllegalStateException - If the type is LocalDate or ZoneId.
      • toLocalTime

        public java.time.LocalTime toLocalTime()
        Gets the current value as a LocalTime with potential forced conversion. The conversion does not lose any time information that LocalTime cannot store.
        Returns:
        The time as LocalTime converted if required.
        Throws:
        java.lang.IllegalStateException - In case the type is LocalDate or ZoneId.
      • toOffsetTime

        public java.time.OffsetTime toOffsetTime​(LocaleInfo localeInfo)
        Gets the current value as a OffsetTime with potential forced conversion. The conversion does not lose any time information that OffsetTime cannot store apart of the calculated zone offset from the current user session or the system default.
        Parameters:
        localeInfo - The locale information to use, or null to use the current user's or system default. Only the conversion from LocalTime or LocalDateTime uses the locale information.
        Returns:
        The time as OffsetTime converted if required.
        Throws:
        java.lang.IllegalStateException - In case the type is LocalDate or ZoneId
      • toLocalDate

        public java.time.LocalDate toLocalDate()
        Gets the date as a LocalDate with potential forced conversion.
        Returns:
        The date as LocalDate converted if required.
        Throws:
        java.lang.IllegalStateException - In case the type is LocalTime, OffsetTime or ZoneId.
      • toLocalDateTime

        public java.time.LocalDateTime toLocalDateTime()
        Gets the date as a LocalDateTime with potential forced conversion.
        Returns:
        The date as LocalDateTime converted if required.
        Throws:
        java.lang.IllegalStateException - In case the type is a LocalTime, OffsetTime, LocalDate or ZoneId.
      • toOffsetDateTime

        public java.time.OffsetDateTime toOffsetDateTime​(LocaleInfo localeInfo)
        Gets the date as a OffsetDateTime with potential forced conversion.
        Parameters:
        localeInfo - The locale information to use, or null to use the current user's or system default.
        Returns:
        The date as OffsetDateTime converted if required.
        Throws:
        java.lang.IllegalStateException - In case the type is a LocalTime, OffsetTime, LocalDate or ZoneId.
      • toZonedDateTime

        public java.time.ZonedDateTime toZonedDateTime​(LocaleInfo localeInfo)
        Gets the date as a LocalDateTime with potential forced conversion, and will only work to convert an OffsetDateTime. All other conversions will fail with an exception.
        Parameters:
        localeInfo - The locale information to use, or null to use the current user's or system default.
        Returns:
        The date as ZonedDateTime converted if required.
        Throws:
        java.lang.IllegalStateException - In case the type is a LocalTime, OffsetTime, LocalDate or ZoneId.
      • toType

        public GDateTime toType​(GDateTime.Type type,
                                LocaleInfo localeInfo)
        Converts a date/time value to another type.
        Parameters:
        type - The target type.
        localeInfo - The locale information to use, or null to use the current user's or system default.
        Returns:
        A GDateTime value with the requested type, or the same instance in case the type already is of the requested type.
        Throws:
        java.lang.IllegalStateException - In case the type conversion is not possible.