Date Conversion and Null-Safe Parsing

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

api.parseDateOnly(pattern, text)

LocalDate

Error

Error

api.parseDateWithPattern(pattern, text)

java.util.Date, midnight UTC

Error

Error

api.parseDateTime(pattern, text)

DateTime, UTC

Returns null

Returns null

api.parseDate(pattern, text) (deprecated)

java.util.Date, UTC

Returns null

Returns null

Groovy Date.parse(pattern, text)

java.util.Date, server time zone

NullPointerException

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 for null, 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:

Groovy
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:

Groovy
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:

Groovy
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(...) in sortBy puts null values 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.

Groovy
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

DateTime

LocalDate

api.parseDateOnly("yyyy-MM-dd", dateTime.toString("yyyy-MM-dd"))

java.util.Date

LocalDate

api.parseDateOnly("yyyy-MM-dd", date.format("yyyy-MM-dd"))

java.sql.Date

LocalDate

sqlDate.toLocalDate()

LocalDate

java.util.Date

api.parseDateWithPattern("yyyy-MM-dd", localDate.toString())

LocalDate

DateTime

api.parseDateTime("yyyy-MM-dd", localDate.toString())

LocalDate

Text

localDate.toString(), for example 2026-09-21

  • dateTime.toString("yyyy-MM-dd") uses the time zone of the DateTime value.

  • date.format("yyyy-MM-dd") uses the time zone of the server.

  • LocalDate to java.util.Date or DateTime gives midnight UTC.

  • api.getDatamartContext() queries return java.sql.Date for the date fields of Data Sources and Datamarts. QueryAPI returns java.time.LocalDate for 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.

See Also