Skip to main content
Version: 4.14.4

Using time

It is possible that you will have to deal with the date and time in an integration flow. Sometimes a specific date or time must be requested, but it may also be possible that an existing date or time must be converted to another type. Below are a number of examples of the use of date and time using simple expressions and Groovy script. Of course date and time transformations are possible by using xslt as well.

Simple expression examples

Here we show multiple examples that generate a timestamp in multiple formats.

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 *

* Only s (seconds), m (minutes) and H (hours) work for 'offsetting' timestamps

Groovy script examples

Example Groovy scripts to transform a header value into a specific date time format. These can be used in the script component

Example 1

Header:

  • Name: value1
  • Value: 20150702

Script:


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

Result: 2015-07-02T00:00:00.000

Example 2

Header:

  • Name: value2
  • Value: 05-04-2019

Script:


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

Result: 20190405

Example 3

Header:

  • Name: value3
  • Value: 1541759458

Script:


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

Result: 1970-01-18T20:15:59.458+0000

Example 4

Header:

  • Name: value4
  • Value: a201805041235040000000

Script:


oldDate = request.getHeader('value4')
timestamp = Date.parse(“yyyyMMddHHmmss”, oldDate).format("yyyy-MM-dd'T'HH:mm:ssZ")
request.setHeader('value4', timestamp)

Result: 2019-08-10T11:41:40+0000

Example 5

Header:

  • Name: value5
  • Value: 1988-06-22 20:15:59.458

Script:


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

Result: Wed Jun 22 20:15:59 UTC 1988

Example 6

Calculate a UNIX timestamp in milliseconds and set it on a header.

Script:


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

Example 7

Calculate an interval in seconds between two dates.

Header:

  • Name: previous
  • Value: 2021-05-25 13:51:18

Script:


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

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

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

Example 8

Formatting & Calculating with date:time.

Header:

  • Name: value8
  • Value: 2023-01-10 12:00:00

Script:


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

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

// 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("value8", newTimestamp);

Result: 10/01/23 12:00:01

Date Format Pattern Syntax

You can design your own format patterns for dates and times from the list of symbols in the following table:

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'

Characters that are not letters are treated as quoted text. That is, they will appear in the formatted text even if they are not enclosed within single quotes. The number of symbol letters you specify also determines the format. For example, if the "zz" pattern results in "PDT," then the "zzzz" pattern generates "Pacific Daylight Time." The following table summarizes these rules:

PresentationNumber of SymbolsResult
Text1 - 3Abbreviated form, if one exists
Text>= 4Full 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