Date/Time Plugin
The plugin provides functionality to generate, modify and validate date/time.
Installation
-
Copy the below line to
dependencies
section of the projectbuild.gradle
fileExample 1. build.gradleimplementation(group: 'org.vividus', name: 'vividus-plugin-datetime', version: '0.5.1')
-
If the project was imported to the IDE before adding new plugin, re-generate the configuration files for the used IDE and then refresh the project in the used IDE.
Properties
Optional
location.zone-id
- Defines time zone which will be used. The default is set to a system default timezone. Time zone format
Expressions
The expression parameters marked with bold are mandatory. |
generateDate
Generates the date using current date as a baseline.
#{generateDate($period, $outputFormat)}
-
period
- the period in ISO-8601 format -
outputFormat
- the output format based on the date/time format
Properties
Property name | Acceptable values | Default | Description |
---|---|---|---|
|
2-letter language code followed by underscore |
|
The locale used for the resulting date. For example, the date '2022-08-11' with format 'EEE, dd MMM yyyy' will be displayed as
|
Let’s imagine the current date is 2020-11-23
and the time is 00:10:01
. Here is a set of examples with the results and comments.
Expression | Result | Comment |
---|---|---|
|
2020-11-23 |
Current date. |
|
2020-11-24T02:10:01 |
Current date +1 day and +2 hours |
|
2020-11-23 |
Current date -1 day |
|
2020-12-23 |
Current date + 1 month |
|
2020-11-23T00:00:01 |
Current date time -10 mins |
|
2020-11 |
Current date with the custom output format |
|
11/23/2020 |
Current date with a custom format |
|
20-11-24 |
Current date +1 day with the custom output format |
|
2020-11-23T00:10:01+0300 |
Current date with the custom output format including time zone offset |
formatDate
Formats the input date to another format. Additionally, time zone can be changed in the output result.
#{formatDate($inputDate, $outputFormat, $outputTimeZone)}
-
$inputDate
- the date to be formatted, it should be presented in the ISO-8601 format -
$outputFormat
- the outut format based on the date/time format -
$outputTimeZone
- the time zone for the output result
Expression | Result |
---|---|
|
2017-01-13T09:00:42.862 |
|
2017-01-13T09:00:42.862 |
|
Throw exception |
|
2017-01-13T09:00:42 |
|
2017-01-13T09:00:42+0000 |
|
2017-01-13T09:00:42-05:00 |
|
2017-01-13 |
|
2017-01-13T04:00:42.862-0500 |
|
2017-01-13T09:00:42.862+0000 |
|
2017-01-13T04:00:42.862-0500 |
|
Error: invalid input data |
|
Error: invalid output format |
|
Error: invalid output timezone |
formatDateTo
Formats the input date to the desired format. The format of the input date should be provided in the second parameter.
#{formatDateTo($inputDate, $inputFormat, $outputFormat)}
-
$inputDate
- the date to be formatted, it can be in any format but this format should be described in the second parameter -
$inputFormat
- the input date format based on the date/time format -
$outputFormat
- the output date format based on the date/time format
Since parameters of the expression comma-separated, meaningful commas (like, parts of the date format pattern) should be escaped using \ .
|
Expression | Result |
---|---|
|
Fri, 31 Mar 2017 07:20:43 GMT |
|
2017-03-31T07:20:43 |
shiftDate
Shifts the given input date for a given period in any direction (+/-).
#{shiftDate($inputDate, $inputFormat, $period)}
-
$inputDate
- the date to be shifted, it can be in any format but this format should be described in the second parameter -
$inputFormat
- the input date format based on the date/time format -
$period
- the period by which the date will be shifted, it should be written in ISO-8601
Since parameters of the expression comma-separated, meaningful commas (like, parts of the date format pattern) should be escaped using \ .
|
Expression | Result |
---|---|
|
2018-03-19 01:40:00 |
|
Fri, 10 May 2019 09:20:43 GMT |
|
"Fri, 10 May, 2019, 09:20:43 GMT" |
diffDate
Calculates the difference between two dates with given formats and returns result as ISO-8601 period.
#{diffDate($inputDate1, $inputDate1Format, $inputDate2, $inputDate2Format)}
-
$inputDate1
- the first input date, it can be in any format but this format should be described in the second parameter -
$inputDate1Format
- the first input date format based on the date/time format -
$inputDate2
- the second input date, it can be in any format but this format should be described in the second parameter -
$inputDate2Format
- the second input date format based on the date/time format
Since parameters of the expression comma-separated, meaningful commas (like, parts of the date format pattern) should be escaped using \ .
|
Expression | Result |
---|---|
|
PT0S |
|
PT0S |
|
PT8774591H59M20S |
toEpochSecond
Converts the input date to the unix epoch time (the number of seconds from the epoch of 1970-01-01T00:00:00Z).
#{toEpochSecond($inputDate)}
-
$inputDate
- the date to be formatted, it should be presented in the ISO-8601 format
Expression | Result |
---|---|
|
734918400 |
|
1607692385 |
Steps
Compare date and current instant
Validates the difference between the current instant and the provided date in seconds.
Then the date '$date' is $comparisonRule current for $seconds seconds
-
$date
- The date in the ISO-8601 format. -
$comparisonRule
- The comparison rule. -
$seconds
- The expected number of seconds between the current instant and the provided date.
Then the date '2020-06-16T15:25:29.516580Z' is less than current for 10 seconds
Compare two dates
Compares two dates according to the given comparison rule.
Then the date '$date1' is $comparisonRule the date '$date2'
-
$date1
- The first date in the ISO-8601 format. -
$comparisonRule
- The comparison rule. -
$date2
- The second date in the ISO-8601 format.
Then the date '2021-04-20T01:02:03.004Z' is greater than the date '2021-04-20T01:02:03.003Z'
Then the date '2021-04-19' is less than the date '2021-04-20'
Compare date with custom format and current instant
Validates the difference between the current instant and the provided date in seconds using the custom date format.
Then the date '$date' in the format '$format' is $comparisonRule current for $seconds seconds
-
$date
- The date to validate. -
$format
- the date/time format. -
$comparisonRule
- The comparison rule. -
$seconds
- The expected number of seconds between the current instant and the provided date.
Then the date '2020-06-25T00:19:58.890007Z' in the format 'uuuu-MM-dd'T'HH:mm:ss.nnnX' is > current for 10 seconds