InstantUtil

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());

format(dateInstant: Instant, format: String, timeZone?: String): String

  • Formats an Instant object using the specified format pattern.

var instant = Instant.now();
var format = "yyyy-MM-dd HH:mm:ss";
var formattedInstant = InstantUtil.format(instant, format);
return "Formatted Instant: " + formattedInstant;

parse(dateStr: String, format: String, timeZone?: String): Instant

  • 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.

Code:

Was this helpful?