[Canonical page](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# 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`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#fromymdhm>), [`fromDateTime`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#fromdatetime>), [`parse`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#parse>) or the [constructor](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#momentlong-minutes>). `Moment` is immutable.

## Declaration

```java
public final class Moment implements Comparable<Moment>
```

## Constructors

### Moment(long minutes)

```java
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

```java
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

```java
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

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

Two moments are equal when their `minutes` are equal.

### toString

```java
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()`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#minvalue-maxvalue>) and [`maxValue()`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#minvalue-maxvalue>).

## Static methods

### fromYmdHm

```java
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

```java
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

```java
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`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#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

```java
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()`](<https://docs.dhtmlx.com/planning-engine/java/api/data/moment.md#tostring>) throws on them.

**Returns** - a new `Moment`.

## Example

```java
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](<https://docs.dhtmlx.com/planning-engine/java/records-ids-and-dates.md>).

## Related

- [`TaskData`](<https://docs.dhtmlx.com/planning-engine/java/api/data/task-data.md>) - the date fields of a task
- [`Worktime`](<https://docs.dhtmlx.com/planning-engine/java/api/data/worktime.md>) - calendar exceptions keyed by date
- [Moments](<https://docs.dhtmlx.com/planning-engine/java/records-ids-and-dates.md#moments>) - building, converting and comparing moments
- [Ids and dates](<https://docs.dhtmlx.com/planning-engine/java/scheduling/project-data.md#ids-and-dates>) - dates in project data
