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 timeLocalTime
- represents time onlyDateTime
- represents date and time including the time zone
On this page:
Function | Description | |
---|---|---|
| Converts the passed def a = date('2011-01-01') def b = date(value(1)) | |
LocalDate date(Partial value) | Converts the passed Partial def a = date(firstValue('L_DATE')) def b = date(lastValue('L_DATE')) | |
LocalDate date(long value) | Converts the passed timestamp 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 | |
| Works in the same way as | |
| Converts the passed def a = time('11:00:23') def b = time(value(1)) | |
LocalTime time(Partial value) | Converts the passed Partial def a = time(firstValue('L_TIME')) def b = time(lastValue('L_TIME')) | |
LocalTime time(long value) | Converts the passed timestamp
| |
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 | |
| Works in the same way as | |
| Converts the passed 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 def a = datetime(firstValue('L_DATETIME')) def b = datetime(lastValue('L_DATETIME')) | |
DateTime datetime(long value) | Converts the passed timestamp 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 | |
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 |
---|---|
| Extracts the year of the def a = date('2011-01-01') return year(a);//returns 2011 |
| Extracts the month from the |
| Extracts the week from the |
| Extracts the day of week from the |
| Extracts the day of month from the |
| Extracts the day of year from the |
| Extracts the hour of the day from the |
| Extracts the hour of the day from the |
| Extracts the minute of hour from the |
| Extracts the minute of hour from the |
| Extracts the minute of the day from the |
| Extracts the second of minute from the |
| Extracts the second of minute from the |
| Extracts the second of day from the |
Comparing two instances
Basic comparison is possible by using the operators ==, =<, <, =>, >, !=
Function | Description | |
---|---|---|
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Determines whether the | |
| Returns the number of seconds between the two specified //returns number of seconds between 00:30:32 and time value in column 1. secondsBetween(time('00:30:32'), time(value(1)) | |
| Returns the number of seconds between the two specified | |
| Returns the number of minutes between the two specified | |
| Returns the number of minutes between the two specified | |
| Returns the number of hours between the two specified | |
| Returns the number of hours between the two specified | |
| Returns the number of days between the two specified //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()) | |
| Returns the number of days between the two specified Example:
| |
| Returns the number of days between the two specified | |
| Returns the number of weeks between the two specified | |
| Returns the number of weeks between the two specified | |
| Returns the number of months between the two specified | |
| Returns the number of months between the two specified | |
| Returns the number of years between the two specified | |
| Returns the number of years between the two specified |
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
andDateTime
- days, weeks, months, yearsLocalTime
andDateTime
- hours, minutes, seconds
Function | Description |
---|---|
| Adds the amount of specified |
| Adds the amount of specified |
| Adds the amount of specified |
| Subtracts the amount of specified |
| Subtracts the amount of specified |
| Subtracts the amount of specified |
Formatting date time output
Function | Description |
---|---|
| Outputs the |
| Outputs the |
| Outputs the |
String
based date time functions
Basic date time functions works with date time as Strings
.
Function | Description |
---|---|
| Returns the current date in |
| Works like the return actualDate('yyyy-MM-dd');//returns date as 2011-12-15 |
| Returns the current time in |
| Works like the 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 return actualDateTime('yyyy-MM-dd HH:mm:ss.SSS');//returns time as 2011-12-15 13:44:22.124 string |
| Returns the part of the passed date or time string. This string should be in format |
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;