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#
Constructors#
Moment(long minutes)#
public
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
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
public int
Two moments are equal when their minutes are equal.
toString#
public String
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
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
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
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, so2026-8-1 9:5is 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
public static Moment
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#
;
;
For timezones, LocalDateTime conversion and date keys, see Records, ids and dates.
Related#
TaskData- the date fields of a taskWorktime- calendar exceptions keyed by date- Moments - building, converting and comparing moments
- Ids and dates - dates in project data