Groovy Library

Updated 

Overview

When developing Guided workflows, configurators need to execute certain groovy operations. In order to assist them with performing these groovy operations more easily, a groovy library has been created. This library explains all the functions that one may come across while working with groovy.

One easy way to test out the Groovy scripts is by using the online Groovy compiler available at https://www.tutorialspoint.com/execute_groovy_online.php.

Fetching/ Modifying Date Variables

Convert from Date to String

You can use this code in scenarios where you need to display date and time information in a specific format, such as logging or displaying in a Screen.

For example, if you are building a flow that processes transactions and need to display the transaction date and time in a specific format in the user interface, you can use this code to convert the date object to a string and display it as needed.

The format string in the example "yyyy-MM-dd HH:mm:ss" specifies that the output string should have year, month, day, hour, minute, and second values separated by dashes and colons. You can modify this format string to match the specific format required in your application.

Replace date field with the field which stores in transaction date in the flow.

Refer to other Timezone Format here: https://mkyong.com/java/java-display-list-of-timezone-with-gmt/

Date date = new Date();

String dateString = date.format("yyyy-MM-dd HH:mm:ss");

return dateString;

Convert Date String Format

This code snippet can be used when you need to convert a string representation of a date and time into a Date object in your flow. It is particularly useful when you have a date string in a specific format, and you need to convert it to a Date object to perform further operations on it.

In the code, the variable dateString represents the date string that needs to be converted to a Date object. You can replace this with the name of the field that contains the date string in your flow.

The Date.parse() method is used to parse the date string into a Date object. The first argument specifies the format of the date string, and the second argument is the actual date string. In this case, the format of the date string is "yyyy/MM/dd HH:mm:ss", which means that the date string should have the format "year/month/day hour:minute:second".

After the date string is parsed into a Date object, it is stored in the date variable. This Date object can then be used in your flow for further processing.

String dateString = "2022/03/01 12:00:00";

Date date = Date.parse("yyyy/MM/dd HH:mm:ss", dateString);

return date;

Convert date to epoch

This code is used to convert the current date and time into Unix epoch time. Unix epoch time is the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC.

In this code, we create a new Date object which represents the current date and time. Then, we use the time method of the Date object to get the number of milliseconds since January 1, 1970, at 00:00:00 UTC. We divide this number by 1000 to convert it into seconds and store it in the epoch variable. Finally, we return the epoch value.

This code can be useful when you need to work with time-based calculations or when you need to communicate with systems that require Unix epoch time. You can replace the new Date() with a specific date object to convert any specific date and time to Unix epoch time. 

Date date = new Date();

long epoch = date.time / 1000;

return epoch;

Convert Date to different time zones

Use this code when you need to convert a date into a formatted string representation, including the time zone. The code uses the Date class to get the current date and time, and then passes it to the  DATE_UTIL.EPOCH_TO_DATE() function along with the desired format "dd-MM-yyyy", time zone "Asia/Kolkata", and language "en". The function converts the date to the specified time zone, formats it according to the specified format, and returns the resulting string. You should replace the date variable with the actual date object you want to convert, and adjust the format, time zone, and language parameters as needed.

Date date = new Date();

return DATE_UTIL.EPOCH_TO_DATE(date,"dd-MM-yyyy","Asia/Kolkata","en")

Difference between two dates

Use this code when you need to calculate the difference between two dates in milliseconds. Make sure to replace date1 and date2 with the names of the variables you want to compare.

Date date1 = new Date();

Date date2 = new Date();

long diffInMilliseconds = date1.time - date2.time;

return diffInMilliseconds;

Adding days to a given date

This code can be useful when you need to calculate a future date by adding a certain number of days to the current date. For example, if you have a task that is due in a week's time, you can use this code to calculate the due date. You can also use this code to schedule events or tasks that occur a certain number of days from the current date.

Make sure to replace daysToAdd with the actual number of days you want to add and also replace date with the name of the variable representing the date in your code.

Date date = new Date();

int daysToAdd = 7;

date = date + (daysToAdd * 24 * 60 * 60 * 1000);

return date;

Fetch the current time and date

This code returns the current date and time. It is useful when you need to get the current date and time in your code. The returned value is an object of the Date class, which represents a specific instant in time. You can use this object to perform various operations such as date formatting, time zone conversions, and date arithmetic.

Date date = new Date();

return date;

String Functions

Adding a few characters to a string

This code can be used whenever there is a need to combine two or more strings into a single string. It can be useful in scenarios such as displaying messages or creating dynamic file names where a combination of text is required.

String str = "Hello";

String newStr = str + " World!";

return newStr;

Capitalization

This code can be used when you need to format a string in a specific way. For example, you may want to capitalize the first letter of a user's name when displaying it on a webpage or in an email. By using the capitalize() method, you can easily achieve this formatting without having to manually manipulate the string.

String str = "hello world";

String capitalizedStr = str.capitalize();

return capitalizedStr;

To Lower Case

The toLowerCase() method returns a new string with all the characters converted to lowercase. This method can be useful when dealing with strings that have inconsistent casing or when we want to make sure that all the characters in a string are in lowercase.

Use this code when you need to convert a string to lowercase. For example, when comparing two strings, it is often useful to convert both strings to lowercase before comparing them. This ensures that the comparison is case-insensitive and will work correctly regardless of the casing of the input strings.

String str = "Hello World"