Skip to content

Dates & Time

Following functions help you write and shorten SpEL when you need to handle time or date values.

currentTimeMillis๐Ÿ”—

String currentTimeMillis()

Returns a String of the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

See System.currentTimeMillis() for further details

Returns :

  • A String of the current time in milliseconds

Examples :

SpEL without : ${T(java.util.String).valueOf(T(java.lang.System).currentTimeMillis())}

SpEL with : ${#currentTimeMillis()}

date๐Ÿ”—

Temporal date(String date, String... format)

See Date(Temporal) & DateTimeFormatter.parseBest() for further details

Parameters :

  • String date : The date you want to get a Temporal from
    • ex. "27 July 2022"
  • String format : The format used for the date (optional, default to ISO)
    • ex. "dd MMMM yyyy"

Returns : The given date as a Temporal

Examples :

SpEL without : ${T(java.time.format.DateTimeFormatter).ofPattern(T(java.time.format.DateTimeFormatter).ISO_INSTANT).parseBest("27 July 2022", ZonedDateTime::from, LocalDateTime::from, LocalDate::from, Instant::from)}

SpEL with : ${#date("27 July 2022")}

dateFormatter๐Ÿ”—

DateTimeFormatter dateFormatter(String pattern)

Creates a formatter from a given pattern.
ex. Pattern d MMM uuuu will format date 2011-12-03 to 3 Dec 2011.

See DateTimeFormatter.ofPattern() for further details

Returns :

  • A DateTimeFormatter
  • Or throws an IllegalArgumentException if the pattern is not valid.

Examples :

SpEL without : ${T(java.time.format.DateTimeFormatter).ofPattern("d MMM uuuu")}

SpEL with : ${#dateFormatter("d MMM uuuu")}

dateFormatterWithLocale๐Ÿ”—

DateTimeFormatter dateFormatterWithLocale(String pattern, String locale)

Creates a formatter from a given pattern and given locale.

See DateTimeFormatter.ofPattern() for further details

Returns :

  • A DateTimeFormatter
  • Or throws an IllegalArgumentException if the pattern is not valid.

Examples :

SpEL without : ${T(java.time.format.DateTimeFormatter).ofPattern("d MMM uuuu", new java.util.Locale("en"))}

SpEL with : ${#dateFormatterWithLocale("d MMM uuuu", "en")}

isoDateFormatter๐Ÿ”—

DateTimeFormatter isoDateFormatter(String type)

See isoDateFormatter(DateTimeFormatter) for further details

Parameters :

  • Possible values are :
    • "INSTANT"
    • "ZONED_DATE_TIME"
    • "DATE_TIME"
    • "DATE"
    • "TIME"
    • "LOCAL_DATE_TIME"
    • "LOCAL_DATE"
    • "LOCAL_TIME"
    • "OFFSET_DATE_TIME"
    • "OFFSET_DATE"
    • "OFFSET_TIME"
    • "ORDINAL_DATE"
    • "ISO_WEEK_DATE"
    • "BASIC_DATE"
    • "RFC_DATE_TIME"

Returns :

  • A DateTimeFormatter
  • Or throws an IllegalArgumentException if the value is unknown.

Examples :

SpEL without : ${T(java.time.format.DateTimeFormatter).ISO_INSTANT}

SpEL with : ${#isoDateFormatter("INSTANT")}

now๐Ÿ”—

ZonedDateTime now()

Returns the current date-time from the system clock.

See ZonedDateTime.now() for further details

Returns :

  • The current date-time as a ZonedDateTime

Examples :

SpEL without : ${T(java.time.ZonedDateTime).now()}

SpEL with : ${#now()}

timeAmount๐Ÿ”—

TemporalAmount timeAmount(String text)

Create a TemporalAmount from a given string.
This is usefull when combine with other methods or functions.

See timeAmount(TemporalAmount) for further details

Returns :

  • A TemporalAmount

Examples :

SpEL without : ${#now().plus(T(java.time.Duration).parse("6 hours"))}

SpEL with : ${#now().plus(#timeAmount("6 hours"))}

timeUnit๐Ÿ”—

ChronoUnit timeUnit(String unit)

See timeUnit(ChronoUnit) for further details

Parameters :

  • Possible values are :
    • "nanos", "ns"
    • "micros", "ยตs"
    • "millis", "ms"
    • "seconds", "s", "sec"
    • "minutes", "m", "min"
    • "hours", "h", "hour", "hours", "hour(s)"
    • "days", "d", "day", "days", "day(s)"
    • "weeks"
    • "months"
    • "years"
    • "decades"
    • "centuries"
    • "millennia"
    • "eras"
    • "forever"

Returns :

  • A ChronoUnit

Examples :

SpEL without : ${T(java.time.temporal.ChronoUnit).valueOf("hours".toUpperCase())}

SpEL with : ${#timeUnit("h")}

zoneRules๐Ÿ”—

ZoneRules zoneRules(String zoneId)

Gets the time-zone rules for this zone id allowing calculations to be performed.

See ZoneId.of(zoneId) and ZoneId.getRules() for further details

Parameters :

  • String zoneId : The zone id you want to get rules from
    • ex. "GMT", "Z", "+02:00"

Returns :

  • A ZoneRules

Examples :

SpEL without : ${T(java.time.ZoneId).of('Z').getRules().nextTransition(T(java.time.Instant).now())}

SpEL with : ${#zoneRules('Z').nextTransition(#now())}

systemZoneRules๐Ÿ”—

ZoneRules systemZoneRules()

Gets the time-zone rules for the system default zone id allowing calculations to be performed.

See ZoneId.systemDefault() and ZoneId.getRules() for further details

Returns :

  • A ZoneRules

Examples :

SpEL without : ${T(java.time.ZoneId).systemDefault().getRules().nextTransition(T(java.time.Instant).now())}

SpEL with : ${#systemDefault().nextTransition(#now())}