Class TimestampString

java.lang.Object
org.apache.calcite.util.TimestampString
All Implemented Interfaces:
Comparable<TimestampString>

public class TimestampString extends Object implements Comparable<TimestampString>
Timestamp literal.

Immutable, internally represented as a string (in ISO format), and can support unlimited precision (milliseconds, nanoseconds).

  • Field Details

  • Constructor Details

    • TimestampString

      public TimestampString(String v)
      Creates a TimeString.
    • TimestampString

      public TimestampString(int year, int month, int day, int h, int m, int s)
      Creates a TimestampString for year, month, day, hour, minute, second, millisecond values.
  • Method Details

    • withMillis

      public TimestampString withMillis(int millis)
      Sets the fraction field of a TimestampString to a given number of milliseconds. Nukes the value set via withNanos(int).

      For example, new TimestampString(1970, 1, 1, 2, 3, 4).withMillis(56)

      Throws:
      IllegalArgumentException - if millis is outside the allowed range
    • withNanos

      public TimestampString withNanos(int nanos)
      Sets the fraction field of a TimestampString to a given number of nanoseconds. Nukes the value set via withMillis(int).

      For example, new TimestampString(1970, 1, 1, 2, 3, 4).withNanos(56789) yields TIMESTAMP '1970-01-01 02:03:04.000056789'.

      Throws:
      IllegalArgumentException - if nanos is outside the allowed range
    • withFraction

      public TimestampString withFraction(String fraction)
      Sets the fraction field of a TimestampString. The precision is determined by the number of leading zeros. Trailing zeros are stripped.

      For example, new TimestampString(1970, 1, 1, 2, 3, 4).withFraction("00506000") yields TIMESTAMP '1970-01-01 02:03:04.00506'.

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

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

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

      public int compareTo(TimestampString o)
      Specified by:
      compareTo in interface Comparable<TimestampString>
    • fromCalendarFields

      public static TimestampString fromCalendarFields(Calendar calendar)
      Creates a TimestampString from a Calendar.
    • round

      public TimestampString round(int precision)
      Returns this value rounded to precision decimal digits after the point.

      Uses rounding mode RoundingMode.DOWN.

    • getMillisSinceEpoch

      public long getMillisSinceEpoch()
      Returns the number of milliseconds since the epoch.
    • fromMillisSinceEpoch

      public static TimestampString fromMillisSinceEpoch(long millis)
      Creates a TimestampString that is a given number of milliseconds since the epoch.
    • toCalendar

      public Calendar toCalendar()
    • toCalendar

      public Calendar toCalendar(TimeZone timeZone)
    • toString

      public String toString(int precision)
      Converts this TimestampString to a string, truncated or padded with zeros to a given precision.