Date conversion functions

You can use the date conversion functions to convert the date or time from one format to another.

Warning Using negation (!) at the beginning of any function can cause unexpected validation results.
Implementation Purpose

Copy code

Long toTimestamp(String dateConstant) {
	LocalDateTime.of(
		LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse(dateConstant)),
		LocalTime.of(0, 0, 0)
	).atOffset(ZoneOffset.UTC).toInstant().toEpochMilli()
}

Converts the date constant to the time stamp.

  • @param dateConstant: The date in the yyyy-MM-dd format.
  • @return: The time stamp of the date constant.

Copy code

String toDateConstant(Long timestamp) {
	Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.UTC)
		.format(DateTimeFormatter.ISO_LOCAL_DATE)
}

Converts the time stamp to a date string in the yyyy-MM-dd format.

  • @param timestamp: The time stamp to convert.
  • @return: The string representation of the time stamp.

Copy code

String toDateConstant(Long timestamp, String pattern) {
	Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.UTC)
		.format(DateTimeFormatter.ofPattern(pattern, ValidationConfig.getInstance().getLocale()))
}

Converts the time stamp to a string using the given pattern.

  • @param timestamp: The time stamp to convert.
  • @param pattern: The pattern to use for conversion, for example, yyyy-MM-dd.
  • @return: The string representation of the time stamp in the given pattern representation.

Copy code

String now() {
	Instant.now().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE)
}

Returns the current date string in the yyyy-MM-dd format.

  • @return: The current date in the yyyy-MM-dd format.