Skip to main content
Version: 4.17.1

Using time

In an integration flow, you may need to work with dates and times. This could involve retrieving a specific date or time or converting an existing date or time into another format. Below are examples of working with date and time using simple expressions and Groovy scripts.

tip

Alternatively, use the XSLT component or XPath expressions when working with date:time.

Simple expression examples

Here are examples of generating a timestamp in various formats, including basic calculation examples:

ExpressionResult
${date-with-timezone:now:Europe/Amsterdam:yyyy-MM-dd HH:mm:ss}2019-08-16 15:04:37
${date-with-timezone:now:Europe/Dublin:yyyy-MM-dd'T'HH:mm:ss}2019-08-16'T'14:04:37
${date-with-timezone:now:Europe/Lisbon:yyyy-MM-dd HH:mm:ss}2019-08-16 14:04:37
${date-with-timezone:now:Europe/Sofia:yyyy-MM-dd HH:mm}2019-08-16 16:04
${date-with-timezone:now:Europe/Amsterdam:HH:mm:ss:SSS}2019-08-16 16:04:37:387
${date-with-timezone:now:Europe/Amsterdam:dd-MM-yyyy}16-08-2019
${date-with-timezone:now:Europe/Amsterdam:ddMMyyyy}16082019
${date-with-timezone:now:Europe/Amsterdam:yyyyMMdd}20190816
${date:now:yyyy-MM-dd HH:mm}2019-08-16 14:04
${date:now+24h:yyyy-MM-dd HH:mm}2019-08-17 14:04 *
${date:now-1h30m:yyyy-MM-dd HH:mm}2019-08-16 12:34 *
${date:now+120000:yyyy-MM-dd HH:mm}2019-08-16 14:06 **

* Only s (seconds), m (minutes) and h (hours) are valid for timestamp offsets
** Value in milliseconds

Groovy script examples

Below are examples of Groovy scripts for date-time transformations. They can be used in the Script component.

Example 1

Convert yyyyMMdd to ISO8601 date:time. Value time header: 20150702.

String oldDate = request.getHeader('time')
Date date = Date.parse( 'yyyyMMdd', oldDate )
String currentDate = date.format( "yyyy-MM-dd'T'HH:mm:ss.SSS" )
request.setHeader('time', currentDate)

Result time header: 2015-07-02T00:00:00.000.

Example 2

Convert dd-MM-yyy to yyyMMdd. Value time header: 05-04-2019.

String oldDate = request.getHeader('time')
Date date = Date.parse( 'dd-MM-yyyy', oldDate )
String currentDate = date.format( "yyyyMMdd" )
request.setHeader('time', currentDate)

Result time header: 20190405.

Example 3

Convert UNIX Timestamp to ISO8601 date:time. Value time header: 1541759458.

Long unixDate = Long.valueOf(request.getHeader('time'))
Date date = new Date(unixDate)
String currentDate = date.format( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
request.setHeader('time', currentDate)

Result time header: 1970-01-18T20:15:59.458+0000.

Example 4

Use regex to extract a date from a complex string and format it. Value time header: a201805041235040000000.

def oldDate = request.getHeader('time')

def matcher = oldDate =~ /\d{14}/ // Regex looks for exactly 14 consecutive digits
if (matcher.find()) {
def cleanedDate = matcher.group(0)
def timestamp = Date.parse('yyyyMMddHHmmss', cleanedDate).format("yyyy-MM-dd'T'HH:mm:ssZ")
request.setHeader('time', timestamp)
} else {
throw new IllegalArgumentException("No valid date-time string found in: ${oldDate}")
}

Result time header: 2018-05-04T12:35:04+0000

Example 5

Convert ISO8601 date:time to readable string with timezone. Value time header: 1988-06-22 20:15:59.458.

oldDate = request.getHeader('time')
timestamp = Date.parse("yyyy-MM-dd HH:mm:ss", oldDate).format("E MMM dd HH:mm:ss z yyyy")
request.setHeader('time', timestamp)

Result time header: Wed Jun 22 20:15:59 UTC 1988.

Example 6

Calculate the current UNIX timestamp in milliseconds.

def currentDateAsUnixMs = new Date().getTime().toString()
def currentDateAsUnix = currentDateAsUnixMs.substring(0,10)
request.setHeader('time', currentDateAsUnix)

Example result time header: 1733240929.

Example 7

Calculate an interval in seconds between two dates. Value time header: 2021-05-25 13:51:18.

def currentTime = new Date()
String currentTimeFormat= currentTime.format("yyyy-MM-dd HH:mm:ss")

def previousTime = request.getHeader('time')
def oldTime = Date.parse("yyyy-MM-dd HH:mm:ss", previousTime)
def difference = currentTime.getTime() - oldTime.getTime()

interval = Math.round(difference/1000)
request.setHeader('time', interval)

Result time header: 111290597.

Example 8

Calculating with date:time. Value time header: 2023-01-10 12:00:00.

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

def timestamp = request.getHeader("time");

// Parse the timestamp string as a LocalDateTime object
def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
def date = LocalDateTime.parse(timestamp, formatter)

// Add 1 year
date = date.plusYears(1)

// Substract 12 months
date = date.minusMonths(12)

// Add 1 week
date = date.plusWeeks(1)

// Substract 7 days
date = date.minusDays(7)

// Add 1 hour
date = date.plusHours(1)

// Substract 60 minutes
date = date.minusMinutes(60)

// Add 1 second
date = date.plusSeconds(1)

// Format the new timestamp string
formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss")
def newTimestamp = date.format(formatter)

request.setHeader("time", newTimestamp);

Result time header: 10/01/23 12:00:01.

Date Format Pattern Syntax

You can design your custom format patterns for dates and times using the symbols listed in the table below:

SymbolMeaningPresentationExample
Gera designatorTextAD
yyearNumber2009
Mmonth in yearText & NumberJuly & 07
dday in monthNumber10
hhour in am/pm (1-12)Number12
Hhour in day (0-23)Number0
mminute in hourNumber30
ssecond in minuteNumber55
SmillisecondNumber978
Eday in weekTextTuesday
Dday in yearNumber189
Fday of week in monthNumber2 (2nd Wed in July)
wweek in yearNumber27
Wweek in monthNumber2
aam/pm markerTextPM
khour in day (1-24)Number24
Khour in am/pm (0-11)Number0
ztime zoneTextPacific Standard Time
'escape for textDelimiter(none)
'single quoteLiteral'
note

Non-alphabet characters in custom formats are treated as literal text. They will appear in the formatted output even if not enclosed in single quotes.

The amount of symbols determines the format. For instance, A zz pattern results in PDT, whereas zzzz produces Pacific Daylight Time. Refer to the table below for these rules:

PresentationNumber of SymbolsResult
Text1 - 3Abbreviated form, if one exists
Text>3Full form
Numberminimum number of digits is requiredShorter numbers are padded with zeros (for a year, if the count of 'y' is 2, then the year is truncated to 2 digits)
Text & Number1 - 2Number form
Text & Number3Text form
Last update on Dec 3, 2024