Class GDateTime

java.lang.Object
com.iizix.prop.GDateTime
All Implemented Interfaces:
Cloneable

public class GDateTime extends Object implements 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
  • Field Details

    • 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 Details

    • GDateTime

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

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

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

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

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

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

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

    • 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(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(Date timeOrDateTime)
      Gets the nanos of seconds of a time value.
    • fromTime

      public static GDateTime fromTime(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(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(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(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(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(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, String string) throws 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:
      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:
      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(Object o)
      Checks if two date objects are equal.
      Overrides:
      equals in class Object
    • getType

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

      public 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 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 String getValueString()
      Gets the date/time value as a String, i.e. "dateTimeValue.toString()".
      Returns:
      the String as described above.
    • paramString

      protected 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 String toString()
      Returns a string representation of this class instance and its values.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this class instance.
    • getLocalTime

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

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

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

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

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

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

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

      public 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 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:
      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:
      IllegalStateException - If the type is LocalDate or ZoneId.
    • toLocalTime

      public 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:
      IllegalStateException - In case the type is LocalDate or ZoneId.
    • toOffsetTime

      public 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:
      IllegalStateException - In case the type is LocalDate or ZoneId
    • toLocalDate

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

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

      public 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:
      IllegalStateException - In case the type is a LocalTime, OffsetTime, LocalDate or ZoneId.
    • toZonedDateTime

      public 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:
      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:
      IllegalStateException - In case the type conversion is not possible.