The InstantUtil class provides a set of utility methods for working with Instant objects, allowing you to format, parse, and extract various date and time components.
Methods
formatted(dateInstant: Instant): String
Returns the default formatted string representation of an Instant object using the workflow's default formatting.
var formattedInstant =InstantUtil.formatted(Instant.now());
Parses a string representing a date and time into an Instant object using the specified format pattern.
var instantStr ="2023-10-24T15:30:00Z";var format ="yyyy-MM-dd'T'HH:mm:ss'Z'";var parsedInstant =InstantUtil.parse(instantStr, format);return"Parsed Instant: "+ parsedInstant;
getYear(dateInstant: Instant): Integer
Gets the year component from the provided Instant.
getDayOfYear(dateInstant: Instant): Integer
Gets the day of the year from the provided Instant.
getMonthName(dateInstant: Instant): String
Gets the name of the month from the provided Instant.
getMonth(dateInstant: Instant): Integer
Gets the month component from the provided Instant.
getDayOfWeek(dateInstant: Instant): String
Gets the name of the day of the week from the provided Instant.
getDayOfMonth(dateInstant: Instant): Integer
Gets the day of the month from the provided Instant.
getHour(dateInstant: Instant): Integer
Gets the hour component from the provided Instant.
getMinute(dateInstant: Instant): Integer
Gets the minute component from the provided Instant.
getSecond(dateInstant: Instant): Integer
Gets the second component from the provided Instant.
getNano(dateInstant: Instant): Integer
Gets the nanoseconds within the second from the provided Instant.
Examples
Example: Reformat a Date String
Description: In this example, you have retrieved data with a date in a less user-friendly format, such as "2023-10-25T12:22:05.2247439Z." To improve data management and enhance readability, you will reformat the date information and introduce a new 'date' column.
Here we utilize several methods of InstantUtil: parse, getYear, getMonth, getDayOfMonth.
var dayOfYear = InstantUtil.getDayOfYear(Instant.now());
var monthName = InstantUtil.getMonthName(Instant.now());
var month = InstantUtil.getMonth(Instant.now());
var dayOfWeek = InstantUtil.getDayOfWeek(Instant.now());
var dayOfMonth = InstantUtil.getDayOfMonth(Instant.now());
var hour = InstantUtil.getHour(Instant.now());
var minute = InstantUtil.getMinute(Instant.now());
var second = InstantUtil.getSecond(Instant.now());
var nanoseconds = InstantUtil.getNano(Instant.now());
// Reformat a Date String
// Parse the date string into an Instant
var instant = Instant.parse(datetime);
// Extract the year, month, and day components from the Instant
var year = InstantUtil.getYear(instant);
var month = InstantUtil.getMonth(instant);
var day = InstantUtil.getDayOfMonth(instant);
// Create a LocalDate object from the extracted components
var formattedDate = LocalDate.of(year, month, day);
// Now 'formattedDate' contains the date information in a structured, more readable format