Creating Dates and Times

Creating Dates and Times

All date and time types have a now constructor that gives you the current system time:

LocalDateTime.now();
ZonedDateTime.now();

Most can be created directly by providing year, month, day, hour, minute, second, and nanosecond parts. These both create dates for December 25, 2025 at exactly 5:32:12.123456789 AM:

LocalDateTime(2025, 12, 25, 5, 32, 12, 123456789);
ZonedDateTime(2025, 12, 25, 5, 32, 12, 123456789);

Of course you often don’t need to be that precise. You can omit any number of trailing arguments. For example, the date above up to the minute:

LocalDateTime(2025, 12, 25, 5, 32);
ZonedDateTime(2025, 12, 25, 5, 32);

Or even just the year. The unspecified args default to the beginning of the period, so these represent January 1, 2025 at midnight:

LocalDateTime(2025);
ZonedDateTime(2025);

In addition to the above, each type has its own, unique constructors. For example, ZonedDateTime.withZoneId, which lets you specify a time zone instead of using the default:

ZonedDateTime.withZoneId('America/Los_Angeles', 2025, 12, 25, 5, 32);

You can find all of the constructors in the API documentation.