Working with date and time objects

Before you can use advanced functions, the date time information must be converted to one of the following objects:

  • LocalDate - represents the date without time
  • LocalTime - represents time only
  • DateTime - represents date and time including the time zone

On this page:

Function

Description

LocalDate date(String value)

Converts the passed value to LocalDate object. The value must be in one of the following formats: dd.MM.yyyy, dd/MM/yyyy, yyyy-MM-dd. Example:

def a = date('2011-01-01')
def b = date(value(1))
LocalDate date(Partial value)

Converts the passed Partial value to LocalDate object. The Partial value is a return value e.g. from firstValue('L_DATE') or lastValue('L_DATE') methods. Example:

def a = date(firstValue('L_DATE'))
def b = date(lastValue('L_DATE'))
LocalDate date(long value)

Converts the passed timestamp value to LocalDate object. The value must be in valid timestamp format. Example:

def a = date(1396348391)
def b = date(value(1))
LocalDate date(long value, String timezone)

Same as date(long value) but with timezone. Valid timezone IDs are:

1. offset, e.g. +01:00, -09:00 etc

 2. ID, e.g. Europe/London

LocalDate date(String value, String format)

Works in the same way as date(String value), additionally you can specify the format of the value parameter. See section Date and time patterns.

LocalTime time(String value)

Converts the passed value to LocalTime object. The value must be in HH:mm:ss format. Example:

def a = time('11:00:23')
def b = time(value(1))
LocalTime time(Partial value)

Converts the passed Partial value to LocalDate object. The Partial value is a return value e.g. from firstValue('L_TIME') or lastValue('L_TIME') methods. Example:

def a = time(firstValue('L_TIME'))
def b = time(lastValue('L_TIME'))
LocalTime time(long value)
 

Converts the passed timestamp value to LocalTime object. The value must be in valid timestamp format. Example:


def a = time(1396348391)
def b = time(value(1))
LocalTime time(long value)

Same as time(long value) but with timezone. Valid timezone IDs are:

1. offset, e.g. +01:00, -09:00 etc

2. ID, e.g. Europe/London

LocalTime time(String value, String format)

Works in the same way as time(String value), additionally you can specify the format of the value parameter. See section Date and time patterns.

DateTime datetime(String value)

Converts the passed value to DateTime object. The value must be in one of the following formats: dd.MM.yyyy HH:mm:ss, dd/MM/yyyy HH:mm:ss, yyyy-MM-dd HH:mm:ss. Example:

def a = datetime('2011-01-01') //creates date time object with 2011-01-01 00:00:00 value
def b = datetime('2011-01-01 23:32') //creates date time object with 2011-01-01 23:32:00 value
def c = datetime('11:21:33') //creates the date time object with 1970-01-01 11:21:33 value
DateTime datetime(Partial value)

Converts the passed Partial value to LocalDate object. The Partial value is a return value e.g. from firstValue('L_DATETIME') or lastValue('L_DATETIME') methods. Example:


def a = datetime(firstValue('L_DATETIME'))
def b = datetime(lastValue('L_DATETIME'))
DateTime datetime(long value)

Converts the passed timestamp value to DateTime object. The value must be in valid timestamp Example:

def a = datetime(1396349028) //creates date time object with 2011-01-01 00:00:00 value
DateTime datetime(long value, String timezone)

Same as datetime(long value) but with timezone. Valid timezone IDs are:

1. offset, e.g. +01:00, -09:00 etc

2. ID, e.g. Europe/London

DateTime datetime(String value, String format)

Works in the same way as datetime(String value), additionally you can specify the format of the value parameter. See section Date and time patterns.

long timestamp(DateTime value)

Converts the passed DateTime value into timestamp.

DateTime decorateWithTimezone(datetime value)Converts UTC value to user's timezone datetime

After the date time has been converted into appropriate objects, you can use following functions:

Getting parts of date time

Function

Description

Integer year(LocalDate date)

Extracts the year of the LocalDate object. Example:

def a = date('2011-01-01')
return year(a);//returns 2011

Integer month(LocalDate date)

Extracts the month from the LocalDate object.

Integer week(LocalDate date)

Extracts the week from the LocalDate object.

Integer dayOfWeek(LocalDate date)

Extracts the day of week from the LocalDate object.

Integer dayOfMonth(LocalDate date)

Extracts the day of month from the LocalDate object.

Integer dayOfYear(LocalDate date)

Extracts the day of year from the LocalDate object.

Integer hourOfDay(LocalTime time)

Extracts the hour of the day from the LocalTime object.

Integer hourOfDay(DateTime dt)

Extracts the hour of the day from the DateTime object.

Integer minuteOfHour(LocalTime date)

Extracts the minute of hour from the LocalTime object.

Integer minuteOfHour(DateTime date)

Extracts the minute of hour from the DateTime object.

Integer minuteOfDay(DateTime date)

Extracts the minute of the day from the DateTime object.

Integer secondOfMinute(LocalTime date)

Extracts the second of minute from the LocalTime object.

Integer secondOfMinute(DateTime date)

Extracts the second of minute from the DateTime object.

Integer secondOfDay(DateTime date)

Extracts the second of day from the DateTime object.

Comparing two instances

Basic comparison is possible by using the operators ==, =<, <, =>, >, !=

Function

Description

boolean isAfter(LocalTime t1, LocalTime t2)

Determines whether the t1 is after the t2 instance.

boolean isAfter(LocalDate d1, LocalDate d2)

Determines whether the d1 is after the d2 instance.

boolean isAfter(DateTime dt1, DateTime dt2)

Determines whether the dt1 is after the dt2 instance.

boolean isBefore(LocalTime t1, LocalTime t2)

Determines whether the t1 is before the t2 instance.

boolean isBefore(LocalDate d1, LocalDate d2)

Determines whether the d1 is before the d2 instance.

boolean isBefore(DateTime dt1, DateTime dt2)

Determines whether the dt1 is before the dt2 instance.

boolean isEqual(LocalTime t1, LocalTime t2)

Determines whether the t1 is equal to t2 instance.

boolean isEqual(LocalDate d1, LocalDate d2)

Determines whether the d1 is equal to d2 instance.

boolean isEqual(DateTime dt1, DateTime dt2)

Determines whether the dt1 is equal to dt2 instance.

Integer secondsBetween(LocalTime t1, LocalTime t2)

Returns the number of seconds between the two specified LocalTime instances. Example:

//returns number of seconds between 00:30:32 and time value in column 1.
secondsBetween(time('00:30:32'), time(value(1))

Integer secondsBetween(DateTime dt1, DateTime dt2)

Returns the number of seconds between the two specified DateTime instances.

Integer minutesBetween(LocalTime t1, LocalTime t2)

Returns the number of minutes between the two specified LocalTime instances.

Integer minutesBetween(DateTime dt1, DateTime dt2)

Returns the number of minutes between the two specified DateTime instances.

Integer hoursBetween(LocalTime t1, LocalTime t2)

Returns the number of hours between the two specified LocalTime instances.

Integer hoursBetween(DateTime dt1, DateTime dt2)

Returns the number of hours between the two specified DateTime instances.

Integer daysBetween(LocalDate d1, LocalDate d2)

Returns the number of days between the two specified LocalDate instances. Example:

//returns number of days between 2011-01-01 and value in column 1.
daysBetween(date('2011-01-01'), date(value(1))
//returns number of days between the actual date and value in current column
daysBetween(date(actualDate()), date(value())

Integer daysBetween(LocalDate d1, LocalDate d2, int[])

Returns the number of days between the two specified LocalDate instances. Day of the week is referenced by number: 1 is Monday, 7 is Sunday.

Example:


daysBetween(dateValue('L_DATE_FROM'),dateValue('L_DATE_TO'), 6,7)
//return number of days between 2 date values. Excludes Saturdays and Sundays from the calculation.

Integer daysBetween(DateTime dt1, DateTime dt2)

Returns the number of days between the two specified DateTime instances.

Integer weeksBetween(LocalDate d1, LocalDate d2)

Returns the number of weeks between the two specified LocalDate instances.

Integer weeksBetween(DateTime dt1, DateTime dt2)

Returns the number of weeks between the two specified DateTime instances.

Integer monthsBetween(LocalDate d1, LocalDate d2)

Returns the number of months between the two specified LocalDate instances.

Integer monthsBetween(DateTime dt1, DateTime dt2)

Returns the number of months between the two specified DateTime instances.

Integer yearsBetween(LocalDate d1, LocalDate d2)

Returns the number of years between the two specified LocalDate instances.

Integer yearsBetween(DateTime dt1, DateTime dt2)

Returns the number of years between the two specified DateTime instances.

Date time manipulation

Manipulation with DateTime, LocalDate and LocalTime objects is supported via the plus(...) and minus(...) functions. Both functions are changing the passed object and are returning it's changed instance. The mandatory part parameter specifies, which part of the date/time value should be changed. Following parts are supported depending on the instance type:

  • LocalDate and DateTime - days, weeks, months, years
  • LocalTime and DateTime - hours, minutes, seconds

Function

Description

DateTime plus(LocalTime time, String part, int count)

Adds the amount of specified part to the passed LocalTime value.

DateTime plus(LocalDate date, String part, int count)

Adds the amount of specified part to the passed LocalDate value.

DateTime plus(DateTime dt, String part, int count)

Adds the amount of specified part to the passed DateTime value.

DateTime minus(LocalTime time, String part, int count)

Subtracts the amount of specified part from the passed LocalTime value.

DateTime minus(LocalDate date, String part, int count)

Subtracts the amount of specified part from the passed LocalDate value.

DateTime minus(DateTime dt, String part, int count)

Subtracts the amount of specified part from the passed DateTime value.

Formatting date time output

Function

Description

String toString(DateTime dt, String format)

Outputs the DateTime object in passed format

String toString(LocalDate date, String format)

Outputs the LocalDate object in passed format

String toString(LocalTime time, String format)

Outputs the LocalTime object in passed format

String based date time functions

Basic date time functions works with date time as Strings.

Function

Description

String actualDate()

Returns the current date in dd.MM.yyyy format. This function is suitable also for inducting an current time for periodic import from data source which has no time column.

String actualDate(String format)

Works like the actulaDate() function extended with custumizable output date format. See section Date and time patterns. Example:

return actualDate('yyyy-MM-dd');//returns date as 2011-12-15

String actualTime()

Returns the current time in HH:mm:ss format. This function is suitable also for inducting an current date for periodic import from data source which has no time column.

String actualTime(String format)

Works like the actulaTime() function extended with custumizable output time format. See section Date and time patterns. Example:

return actualDate('HH:mm:ss');//returns time as 13:44:22 string
String actualDateTime() Returns the current date in dd.MM.yyyy HH:mm:ss format. This function is suitable also for inducting current time for periodic import from data source which has no time column.
String actualDateTime(String format)

Works like the actulaDateTime() function extended with customizable output time format. See section Date and time patterns. Example:

return actualDateTime('yyyy-MM-dd HH:mm:ss.SSS');//returns time as 2011-12-15 13:44:22.124 string

String datetimePart(String value, String part)

Returns the part of the passed date or time string. This string should be in format yyyy-MM-dd HH:mm:ss or yyyy-MM-dd. The part parameters defines one of the following: year, monthOfYear, weekOfYear, dayOfMonth, dayOfWeek, dayOfYear, hourOfDay, minuteOfHour, secondOfMinute

Date and Time Patterns

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

Letter

Date or Time Component

Examples

G

Era designator

AD

y

Year

1996; 96

M

Month in year

July; Jul; 07

w

Week in year

27

W

Week in month

2

D

Day in year

189

d

Day in month

10

F

Day of week in month

2

E

Day in week

Tuesday; Tue

a

Am/pm marker

PM

H

Hour in day (0-23)

0

k

Hour in day (1-24)

4

K

Hour in am/pm (0-11)

0

h

Hour in am/pm (1-12)

12

m

Minute in hour

30

s

Second in minute

55

S

Millisecond

978

z

Time zone

Pacific Standard Time; PST; GMT-08:00

Z

Time zone RFC 822

-0800

Another way how to compare two dates

Consider following example:
We have an import, which contains two date columns and we want the day difference of these two dates. Both date columns contains date in format yyyy/MM/dd.

def a1 = split(value(1), '/');
def date1 = [a1[0] as int, a1[1] as int, a1[2] as int] as Date;
def a2 = split(value(2), '/');
def date2 = [a2[0] as int, a2[1] as int, a2[2] as int] as Date;
return date2 - date1;
  • No labels