Can I calculate the difference between any two dates and then trigger an email using Groovy?
Yes, you can use Groovy to trigger an email alert after calculating the difference between two dates.
Here's an example where we'll use Groovy to trigger an email alert after 10 days of
Employee Probation End Date, after calculating the difference between the dates:
-
Create a field in Activity with the name ProbationEndDate.
-
Use the following code to trigger an email alert exactly after 10 days of Employee Probation End Date by calculating the difference:
//Difference between dates //Business Scenario: To trigger an email alert exactly after 10 days of Employee Probation End Date. if(TRUNC(SYSDATE) - Trunc(${ProbationEndDate}) == 10) { // Business logic } //To reference the application server's current date and time in any groovy expression, use: adf.currentDate adf.currentDateTime //To reference the database's current date and time in any groovy expression, use: adf.currentDBDate adf.currentDBDateTime //Returns: the current date, with no time println(today()) //Returns the current date and time println(now()) //1. Difference between two dates def today = new Date() def yesterday = today - 1 assert 1 == today.minus(yesterday) assert 1 == today - yesterday //2. Date.parse() to convert String to Date. def date = new Date().parse('yyyy/MM/dd', '2019/12/09') //3. We can use [] or getAt() to get date fields. assert 2019 == date[Calendar.YEAR] assert 11 == date[Calendar.MONTH] assert 9 == date.getAt(Calendar.DATE) //Returns a number representing the month def currentMonth=month(new Date()) def nextMonth = currentMonth + 1 // We can use the + and - operators to add or subtract days. def date = new Date().parse('yyyy/MM/dd', '2019/07/22') def dateNext = date.clone() +1 // prints --> Tue Jul 23 00:00:00 UTC 2019 def datePrevious = date.clone() -1 // prints --> Sun Jul 21 00:00:00 UTC 2019 def nextDay = date + 1 // Or date.plus(1) def previousDay = date - 1 // Or date.minus(1) // ++ operator to move one day ahead. dateNext++ // Or dateNext.next() assert dateNext == nextDay // -- operator to move one day back. datePrevious-- // Or datePrevious.previous() assert datePrevious == previousDay def otherDate = new Date().parse('yyyy/MM/dd', '2019/07/25') // Dates can be used in ranges. println((otherDate..<date).size()) // prints -->3 // Date.format() uses java.text.SimpleDateFormat. assert '9 December, 2019' == date.format("d MMMM, yyyy") assert '12/9/19' == date.getDateString()