Moment#

A wall-clock date and time at minute precision, with no timezone and no DST. It's stored as a count of minutes since 1970-01-01 00:00. Every date field in the input records and results is a Moment. Build one with fromYmdHm, fromDateTime, parse or the constructor. Moment is immutable.

Declaration#

public final class Moment implements Comparable<Moment>

Constructors#

Moment(long minutes)#

public Moment(long minutes)

Creates a moment from a raw minute count. Any long is accepted, including negative values for dates before 1970.

Parameters

Name Type Description
minutes long Minutes since 1970-01-01 00:00

Fields#

minutes#

public final long minutes;

Minutes since 1970-01-01 00:00. Adding to it moves the moment in wall-clock time, without regard to any calendar: new Moment(m.minutes + 90) is 90 minutes later.

Methods#

compareTo#

public int compareTo(Moment other)

Compares by minutes. Moment sorts in time order and works as a TreeMap or TreeSet key.

Returns - a negative number, zero or a positive number when this moment is earlier than, equal to or later than other.

equals, hashCode#

public boolean equals(Object other)
public int hashCode()

Two moments are equal when their minutes are equal.

toString#

public String toString()

Formats the moment as YYYY-MM-DD HH:MM, for example 2026-08-10 08:30. Years before 0000 or after 9999 carry a sign: -0001-01-01 00:00, +10000-01-01 00:00.

Errors - throws java.time.DateTimeException when the date is outside the java.time.LocalDate range, as with minValue() and maxValue().

Static methods#

fromYmdHm#

public static Moment fromYmdHm(int year, int month, int day, int hour, int minute)

Builds a moment from calendar fields.

Parameters

Name Type Description
year int Year, from −262143 to 262142
month int Month, 1-12
day int Day of the month, valid for that month and year
hour int Hour, 0-23
minute int Minute, 0-59

Returns - the moment, or null when any field is out of range or the date doesn't exist (for example February 29 in a common year). It doesn't throw.

fromDateTime#

public static Moment fromDateTime(java.time.LocalDateTime value)

Builds a moment from the date, hour and minute of a LocalDateTime. Seconds and fractions are dropped. The fields are read as they are, with no zone conversion. Convert zoned times to the plan's local wall-clock time before calling.

Parameters

Name Type Description
value java.time.LocalDateTime The date and time; not null

Returns - the moment.

Errors - throws NullPointerException when value is null.

parse#

public static Moment parse(String value)

Parses a date or a date-time from text. Accepted form: YYYY-M-D, optionally followed by h:m or Th:m, each optionally with :s.

  • The year is 1-4 digits, or a + or - sign followed by digits. Month, day, hour, minute and second are 1-2 digits each, so 2026-8-1 9:5 is accepted.
  • Whitespace is allowed around the whole value and after each - and :.
  • Without a time part, the time is 00:00.
  • Seconds, from 0 to 60, are checked and then dropped.
  • Field ranges follow fromYmdHm.

Parameters

Name Type Description
value String The text to parse; not null

Returns - the moment.

Errors - throws IllegalArgumentException with the message "invalid date" when the text doesn't match the form or a field is out of range. Throws NullPointerException when value is null.

minValue, maxValue#

public static Moment minValue()
public static Moment maxValue()

The earliest and latest representable moments: Long.MIN_VALUE and Long.MAX_VALUE minutes. Both lie outside the LocalDate range, so toString() throws on them.

Returns - a new Moment.

Example#

import org.dhtmlx.plen.*;
import java.time.LocalDateTime;

public class MomentExample {
    public static void main(String[] args) throws Exception {
        Moment start = Moment.fromYmdHm(2026, 8, 10, 8, 30);
        Moment parsed = Moment.parse("2026-8-10T08:30");
        Moment fromJava = Moment.fromDateTime(LocalDateTime.of(2026, 8, 10, 8, 30, 45));

        System.out.println(start);                                 // 2026-08-10 08:30
        System.out.println(start.minutes);                         // 29772510
        System.out.println(start.equals(parsed) && start.equals(fromJava)); // true
        System.out.println(Moment.fromYmdHm(2026, 2, 29, 0, 0));   // null

        Moment end = new Moment(start.minutes + 90);
        System.out.println(end);                                   // 2026-08-10 10:00
        System.out.println(start.compareTo(end) < 0);              // true
    }
}

For timezones, LocalDateTime conversion and date keys, see Records, ids and dates.