Logic code gets dates as text, as java.util.Date, as java.sql.Date, as java.time.LocalDate or as org.joda.time.DateTime. Date and DateTime (Quick Reference) lists the type that each API returns. This page shows how to parse text without errors and how to convert one type to another.
Parse Text to a Date
The parse methods react differently to a missing value and to text that does not match the pattern:
|
Method |
Returns |
Text is null or empty |
Text does not match the pattern |
|---|---|---|---|
|
|
|
Error |
Error |
|
|
|
Error |
Error |
|
|
|
Returns |
Returns |
|
|
|
Returns |
Returns |
|
Groovy |
|
|
Ignores extra text at the end and rolls over values that are out of range |
-
To parse a date, use
api.parseDateOnly. It reports every problem as an error. -
To parse a date and time, use
api.parseDateTime. Check the result fornull, because the method does not report an error. -
Do not use Groovy
Date.parse. It can return a wrong value without an error:-
It ignores extra text at the end of the text.
-
It rolls over values that are out of range. For example,
Date.parse("yyyy-MM-dd", "2026-13-45")returns 2027-02-14.
-
Text With Date and Time
When the text holds a date and a time, for example 2026-09-21T13:05:45, the pattern must cover the whole text:
String text = "2026-09-21T13:05:45"
api.parseDateTime("yyyy-MM-dd'T'HH:mm:ss", text) // 2026-09-21T13:05:45.000Z
api.parseDateOnly("yyyy-MM-dd", text) // error: unparsed text found at index 10
Date.parse("yyyy-MM-dd", text) // 2026-09-21 00:00, the time is lost
To get only the date, parse the first 10 characters:
api.parseDateOnly("yyyy-MM-dd", text.take(10)) // 2026-09-21
Error: Cannot invoke "String.length()" Because "" Is Null
A logic element fails with one of these messages:
Cannot invoke "String.length()" because "<parameter1>" is null
ERROR(@3): Cannot invoke "String.length()" because "" is null
The server sends the first form. Some places, for example the Evaluation failed window in Studio, do not show <parameter1>, so the quotes are empty.
Reason
Groovy Date.parse(pattern, text) received null as the text. This happens when the value is missing, for example an empty field, or an api.local value that the current logic did not set.
Solution
Check the value before you parse it, and decide what the logic does without a date:
String rawDate = api.local.quoteDetails?.createDate as String
LocalDate createdDate = rawDate ? api.parseDateOnly("yyyy-MM-dd", rawDate.take(10)) : null
if (createdDate == null) {
api.addWarning("Quote create date is missing")
return null
}
To sort a list by a date that can be missing:
-
If the rows come from a query, sort them in the query. In QueryAPI,
orders.ascNullsFirst(...)insortByputsnullvalues first. See Reference (QueryAPI) and Performance (QueryAPI). -
If the rows do not come from a query, for example inputs or JSON, sort them in Groovy. Parse each date once before you sort. A comparator that parses the dates runs the parse on every comparison, which is slow for large lists.
List sortedRows = rows
.collect { row -> [row: row, date: row[0] ? api.parseDateOnly("yyyy-MM-dd", row[0]) : null] }
.sort { it.date }
.collect { it.row }
Groovy sorts null values first.
Convert One Date Type to Another
|
From |
To |
Code |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Text |
|
-
dateTime.toString("yyyy-MM-dd")uses the time zone of theDateTimevalue. -
date.format("yyyy-MM-dd")uses the time zone of the server. -
LocalDatetojava.util.DateorDateTimegives midnight UTC. -
api.getDatamartContext()queries returnjava.sql.Datefor the date fields of Data Sources and Datamarts. QueryAPI returnsjava.time.LocalDatefor the same fields.
Do not use dateTime.toLocalDate(). It returns org.joda.time.LocalDate, a different class from java.time.LocalDate. The logic sandbox blocks this class with the error Unexpected type: class org.joda.time.LocalDate.