Skip to Content
Technical Articles
Author's profile photo Raviteja Satuluri

Learning Groovy Script for Cloud Integration – Part 3

Introduction

Here I am just explaining the Importance of Groovy script in the Real time scenarios when we are dealing with Cloud Integration.

Groovy scripting is an integral and important feature of SAP Cloud Platform Integration (CPI). The goals of this repository are: Providing templates when you are implementing a new script. Easily finding Groovy functions related to the CPI topic at hand. Minimizing search engine time for common tasks.

In this blog I focused on Date functions using Java Script

 

Requirement 1:

Finding out Today’s Date

 

Requirement 2:

Date Formatting & Date Parsing

Changing one Date format to another Date format

Sometimes, we need to format date object into desired format. In Groovy, you can use format function to format date object. You can see a quick example below.

Date parsing is the operation of conversion date string to date object. You can simply parse date string to date object easily like below.

 

Requirement 3:

Date Arithmetic 

Adding number of days to the current date and subtracting number of days to the current date

The main advantage of the Groovy date extension is the doing arithmetic operations on date object. Let say that, you have a date and you want to find the date object which is 5 days after from current date and 10 days before the current date In order to do that, you can use following example.

Requirement 4:

Subscript Operators 

In some cases, you may need to get year, month, date values separately from date objects, or you may need to set those fields in date objects separately. Subscript operators help us to get that specific values.

Requirement 5:

Converting Seconds time into complete Time Duration (Days, Hours and Minutes)

In some cases, you may need to convert seconds time in to complete duration for some particular Business requirements. The below example will help you how to get the seconds time in to number of days, number of hours and number of minutes.

Requirement 6:

Showing Current time into multiple Time Zones

In some cases, if you want to populate your current date according to Time Zones, the below example will help you to achieve the requirement.

Requirement 7:

How to add hours and minutes to current date

 

Requirement 8:

Convert a Unix Timestamp to Time

Method 1:

Unix Timestamp Example: 1651822834

 

Method 2:

Input = 1615204493000

Conclusion –

Date operations are the operations that we used during software development, and it is really painful on converting dates from one format to another. In Groovy, these operations are very simplified, and you can easily perform date operations like applying arithmetic in Math. You can parse date string, format a date object, increment – decrement date, and apply subscript operators on date objects without using any third-party libraries.

 

Hope this document will help to beginners to understand CPI concept of Groovy Script.

Happy Learning 🙂

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Andrey Tkachuk
      Andrey Tkachuk

      Hi,

      Thanks for the post.

      Please show how to add hours (not days).

      Author's profile photo Saurabh Kabra
      Saurabh Kabra

      Hi,

      You can check the standard groovy library groovy.time.TimeCategory that can do this. Here is a sample code snippet.

      import com.sap.gateway.ip.core.customdev.util.Message;
      import groovy.time.TimeCategory;
      def Message processData(Message message) {
          use(groovy.time.TimeCategory){
              def dateTime = new Date();
              message.setHeader("currentDateTime",dateTime)
              message.setBody(dateTime - 30.minutes)
          }
          return message;
      }

      Thanks

      Saurabh

      Author's profile photo Raviteja Satuluri
      Raviteja Satuluri
      Blog Post Author

      Hi Andrey,

      Please follow below: