Contact Us 1-800-596-4880

toTime

toTime(str: String, formatters: Array<Formatter>): Time

Transforms a String value into a Time value using the first Formatter that matches with the given value to transform.

Introduced in DataWeave version 2.5.0.

Parameters

Name Type Description

str

String

The String value to transform into a Time value.

formatters

Array<Formatter>

The array of formatting to use on the Time value.

Example

This example shows how toTime behaves with different inputs. It produces output in the application/dw format.

Source

%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
  a: toTime("13:44:12.283-08:00", [{format: "HH:mm:ss.xxx"}, {format: "HH:mm:ss.nxxx"}]),
  b: try(() -> toTime("13:44:12.283-08:00", [{format: "HH:mm:ss.xxx"}]).error.message
}

Output

{
  a: |13:44:12.000000283-08:00| as Time {format: "HH:mm:ss.nxxx"},
  b: "Could not find a valid formatter for '13:44:12.283-08:00'"
}

toTime(str: String, format: String | Null = null, locale: String | Null = null): Time

Transforms a String value into a Time value and accepts a format and locale.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

str

The String value to transform into a Time value.

format

The formatting to use on the Time value. A null value has no effect on the Time value. This parameter accepts Java character patterns based on ISO-8601. A Time value, such as 10:15:30.000000, has the format HH:mm:ss.nxxx.

locale

Optional ISO 3166 country code to use, such as US, AR, or ES. A null or absent value uses your JVM default.

Example

This example shows how toTime behaves with different inputs. It produces output in the application/dw format.

Source

%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
   a: toTime("23:57:59Z"),
   b: toTime("13:44:12.283-08:00","HH:mm:ss.nxxx")
}

Output

{
  a: |23:57:59Z|,
  b: |13:44:12.000000283-08:00| as Time {format: "HH:mm:ss.nxxx"}
}