How to use Groovy to work with Dates and Times

How to use Groovy to work with Dates and Times

Problem

Cycle does not include steps to access and work with Date and Time.

Solution

Cycle now includes steps to execute simple Groovy statements as well as execute complex Groovy scripts. 

The 2 Cycle steps that execute Groovy are: 

I execute Groovy "<GROOVY_STATEMENTS>"
I execute Groovy script "<GROOVY_SCRIPT_FILE>"

This article assumes conceptual familiarity with Groovy/Java including general syntax, data types, import statements, class references and method usage.

For more information on Groovy see Apache Groovy

2 items of note regarding Cycle and Groovy.

  • 1. Cycle can only input and output Strings and Number Data Types with Groovy statements and scripts.
    • Dates, arrays, lists, maps are not yet functional inputs or outputs but can be used within a Groovy script.
  • 2. Cycle outputs a variable $groovy_result which represents the last result
    • Custom variables can be assigned and used

Examples

These examples use classes methods from the improved Java 8+ Date/Time API which replaced deprecated Date and Calendar functionality.

Get current date with a Groovy statement:

@inline_date
Scenario: Date
Given I execute Groovy """import java.time.*; dateStr = LocalDate.now()format("MM/dd/yyyy")"""
Then I echo $dateStr

This example imports the java.time library and in a single statement gets the current date, formats it and converts it to a string for Cycle output. The format used in this example is MM/dd/yyyy but any syntactically valid format can be used. 

A variation on this Scenario is to pass in the format from Cycle as a variable.

@inline_date_variable
Scenario: Date Variable
Given I assign "MM/dd/yyyy" to variable "format"
Then I execute Groovy "import java.time.*; dateStr = LocalDate.now()format(format)"
Then I echo $dateStr

Get current date time with a Groovy statement:

@inline_date_time
Scenario: Date Time
Given I execute Groovy """import java.time.*; dateTimeStr = LocalDateTime.now()format("MM/dd/yyyy HH:mm:ss")"""
Then I echo $dateTimeStr

This example imports the java.time library and in a single statement gets the current date time, formats it and converts it to a string for Cycle output. The format used in this example is MM/dd/yyyy HH:mm:ss but any syntactically valid format can be used.

A variation on this Scenario is to pass in the format from Cycle as a variable.

@inline_date_time_variable
Scenario: Date Time
Given I assign "MM/dd/yyyy HH:mm:ss" to variable "format"
Given I execute Groovy """import java.time.*; dateTimeStr = LocalDateTime.now()format(format)"""
Then I echo $dateTimeStr

Executing Groovy script files

Date Manipulation

The need may arise to not only access and use dates but also manipulate specified dates by creating ranges, date addition/subtraction or reformatting. Since these require multiple steps we will be using Groovy scripts to perform the actions.

In the next example Scenario we will be passing our known date as a variable and the user defined input format into our Groovy script. We will be returning the calculated dates based our user defined days variable in the user defined output format and echoing in the Cycle Output for validation. Once they are returned to Cycle they can be used however desired. 

@script_date
Scenario:Groovy script date
Given I assign "04/30/2019" to variable "dateStr"
And I assign "MM/dd/yyyy" to variable "format"
And I assign "dd-MM-yyyy" to variable "outFormat"
And I assign 7 to variable "days"
When I execute Groovy script "scripts\date_script.groovy" within 8 seconds
Then I echo $plusWeekStr
Then I echo $minusWeekStr

The following date_script.groovy file is what gets executed in the Scenario. We import the necessary libraries to perform the functions needed. As previously mentioned Cycle requires strings and numbers so the next step is to convert the passed in string to a date data type to be manipulated in Groovy. We then add the number of days based on the days variable assigned in Cycle to create a new date variable. Similarly we create another date variable representing the passed in date minus the number of passed in days. The final step is to convert the new date variables into strings to be used as Cycle variables.

import java.text.DateFormat
import java.time.format.DateTimeFormatter
import java.time.*

//Convert string to date
strToDate = LocalDate.parse(dateStr,format)

//Add days value to the date
plusWeek = strToDate + days

//Minus days value from the date
minusWeek = strToDate - days

//Convert dates to strings for Cycle output
plusWeekStr = plusWeek.format(outFormat)
minusWeekStr = minusWeek.format(outFormat)

Date Time Manipulation

In the next example Scenario we will be passing our known date time as a variable and the user defined input format into our Groovy script. We will be returning the calculated date times based our user defined hours or seconds variable in the user defined output format and echoing in the Cycle Output for validation. Once they are returned to Cycle they can be used however desired. 

@script_date_time
Scenario:Groovy script date time
Given I assign "04/30/2019 23:21:45" to variable "dateStr"
And I assign "MM/dd/yyyy HH:mm:ss" to variable "format"
And I assign "yyyy-MM-dd HH:mm:ss" to variable "outFormat"
And I assign 2 to variable "hours"
And I assign 45 to variable "seconds"
Then I execute Groovy script "scripts\date_time_script.groovy" within 8 seconds
Then I echo $plusHourStr
Then I echo $minusSecondsStr

The following date_time_script.groovy file is what gets executed in the Scenario. We import the necessary libraries to perform the functions needed. As previously mentioned Cycle requires strings and numbers so the next step is to convert the passed in string to a date data type to be manipulated in Groovy. We then add the number of hours based on the hours variable assigned in Cycle to create a new date variable. Similarly we create another date variable representing the passed in date time minus the number of passed in seconds. The final step is to convert the new date variables into strings to be used as Cycle variables.

import java.text.DateFormat
import java.time.format.DateTimeFormatter
import java.time.*

//Convert string to date
strToDate = LocalDateTime.parse(dateStr,format)

//Add hours value to the date time
plusHour = strToDate.plusHours(hours)

//Minus seconds value from the date time
minusSeconds = strToDate.minusSeconds(seconds)

//Convert date back to string for Cycle output
plusHourStr = plusHour.format(outFormat)
minusSecondsStr = minusSeconds.format(outFormat)

 

 

 

    • Related Articles

    • How to use Groovy to work with Strings

      Problem Other than concatenate Cycle does not include steps to work with strings. Solution Cycle now includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts. The 2 Cycle steps that execute Groovy are: I ...
    • How to use Groovy to return values in XML strings

      Problem It may be necessary to return values from XML strings in your automated tests. This article provides examples for common ways to use the Groovy programming language to output XML information. Solution Cycle includes steps to execute simple ...
    • How to create methods in Groovy and call from Cycle

      Cycle includes a step to execute Groovy scripts. I execute Groovy script "<GROOVY_SCRIPT_FILE>" Groovy scripts can be constructed to perform multiple functions in a single file by creating user defined methods. Cycle variables can be used to control ...
    • Where can I get help using Groovy with Cycle?

      Cycle now includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts.  The 2 Cycle steps that execute Groovy are:  I execute Groovy "<GROOVY_STATEMENTS>" I execute Groovy script "<GROOVY_SCRIPT_FILE>" Creating ...
    • How to perform basic math in Cycle using Groovy

      Problem Cycle can increment variable values but does not include steps for simple math Solution Cycle now includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts. The 2 Cycle steps that execute Groovy are: ...