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.

var year = InstantUtil.getYear(Instant.now());

getDayOfYear(dateInstant: Instant): Integer

  • Gets the day of the year from the provided Instant.

var dayOfYear = InstantUtil.getDayOfYear(Instant.now());

getMonthName(dateInstant: Instant): String

  • Gets the name of the month from the provided Instant.

var monthName = InstantUtil.getMonthName(Instant.now());

getMonth(dateInstant: Instant): Integer

  • Gets the month component from the provided Instant.

var month = InstantUtil.getMonth(Instant.now());

getDayOfWeek(dateInstant: Instant): String

  • Gets the name of the day of the week from the provided Instant.

var dayOfWeek = InstantUtil.getDayOfWeek(Instant.now());

getDayOfMonth(dateInstant: Instant): Integer

  • Gets the day of the month from the provided Instant.

var dayOfMonth = InstantUtil.getDayOfMonth(Instant.now());

getHour(dateInstant: Instant): Integer

  • Gets the hour component from the provided Instant.

var hour = InstantUtil.getHour(Instant.now());

getMinute(dateInstant: Instant): Integer

  • Gets the minute component from the provided Instant.

var minute = InstantUtil.getMinute(Instant.now());

getSecond(dateInstant: Instant): Integer

  • Gets the second component from the provided Instant.

var second = InstantUtil.getSecond(Instant.now());

getNano(dateInstant: Instant): Integer

  • Gets the nanoseconds within the second from the provided Instant.

var nanoseconds = InstantUtil.getNano(Instant.now());

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:

// 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

Last updated