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.
Expression | Result |
---|---|
${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:ss} | 2019-08-16 16:04:37 |
${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-24H:yyyy-MM-dd HH:mm:ss} | 2019-08-16 14:04:37 |
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:
Symbol | Meaning | Presentation | Example |
---|---|---|---|
G | era designator | Text | AD |
y | year | Number | 2009 |
M | month in year | Text & Number | July & 07 |
d | day in month | Number | 10 |
h | hour in am/pm (1-12) | Number | 12 |
H | hour in day (0-23) | Number | 0 |
m | minute in hour | Number | 30 |
s | second in minute | Number | 55 |
S | millisecond | Number | 978 |
E | day in week | Text | Tuesday |
D | day in year | Number | 189 |
F | day of week in month | Number | 2 (2nd Wed in July) |
w | week in year | Number | 27 |
W | week in month | Number | 2 |
a | am/pm marker | Text | PM |
k | hour in day (1-24) | Number | 24 |
K | hour in am/pm (0-11) | Number | 0 |
z | time zone | Text | Pacific Standard Time |
' | escape for text | Delimiter | (none) |
' | single quote | Literal | ' |
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:
Presentation | Number of Symbols | Result |
---|---|---|
Text | 1 - 3 | Abbreviated form, if one exists |
Text | >= 4 | Full form |
Number | minimum number of digits is required | Shorter numbers are padded with zeros (for a year, if the count of 'y' is 2, then the year is truncated to 2 digits) |
Text & Number | 1 - 2 | Number form |
Text & Number | 3 | Text form |