Get Current Date and time

In Teneo Studio, you can use the following code to get current date during runtime and store the day of month, the month and the year in integer variables iDay , iMonth and iYear respectively (please define them as global variable or flow variable).

def c = Calendar.getInstance()
iDay = c.get(Calendar.DAY_OF_MONTH) // Get current day of month
iMonth = c.get(Calendar.MONTH) + 1 // Get current month, don’t forget +1
iYear = c.get(Calendar.YEAR) // Get current year

The Calendar class used in the code is already included in Groovy, so you do not need to import java.util.Calender before. Please be aware that the range of Calender.MONTH is from 0 to 11. For example, if you are in March now, the value of Calender.MONTH will be 2.

You can also use the following code to get current time in your timezone and store it as a string in variable sTimeString .

import java.text.DateFormat
// Define the locale based on the 2-letter language code of the solution.
def locale = new Locale("en")
// Create time zone
TimeZone tz = TimeZone.getTimeZone("CET")
// Initialize a new calendar object
Calendar cal = Calendar.getInstance(tz)
// Create a format to display the full time.
def timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale)
sTimeString = timeFormat.format(cal.getTime())

You can use a large range of time zone IDs in the code above, such as CET , US/Pacific , GMT+8 etc. Please check the valid time zone ID list using the following code:

import java.time.ZoneId;
Set<String> zoneIds = ZoneId.getAvailableZoneIds();

The code in this post is just an example on how you can get access to the current date and time in your Teneo solution. If you are interested in advanced Date and Time handling, please take a look at the DateTime Handler. I hope this code example comes in handy for your project!