SpEL Functions

Office-stamper extends the Spring Expression Language (SpEL) with additional functions that you can use in your templates. These functions provide convenient ways to format dates, manipulate strings, and perform other common operations.

Date and Time Formatting Functions

Office-stamper provides a rich set of functions for formatting date and time objects:

Function Description
fdate(date) Formats a date using ISO_OFFSET_DATE, (for example "2000-01-12+02:00".)
fdatetime(date) Formats a date-time using ISO_ZONED_DATE_TIME, (for example "2000-01-12T23:34:45.000000567+02:00[UTC+02:00]".)
ftime(date) Formats a time using ISO_OFFSET_TIME, (for example "23:34:45.000000567+02:00".)
finstant(date) Formats a date-time as an instant in UTC, (for example "2000-01-12T21:34:45.000000567Z".)
fbasicdate(date) Formats a date using BASIC_ISO_DATE, (for example "20000112+0200".)
fordinaldate(date) Formats a date using ISO_ORDINAL_DATE, (for example "2000-012+02:00".)
fweekdate(date) Formats a date using ISO_WEEK_DATE, (for example "2000-W02-3+02:00".)
f1123datetime(date) Formats a date-time using RFC_1123_DATE_TIME, (for example, "Wed, 12 Jan 2000 23:34:45 +0200".)
foffsetdate(date) Formats a date using ISO_OFFSET_DATE, (for example "2000-01-12+02:00".)
foffsetdatetime(date) Formats a date-time using ISO_OFFSET_DATE_TIME, (for example "2000-01-12T23:34:45.000000567+02:00".)
foffsettime(date) Formats a time using ISO_OFFSET_TIME, (for example "23:34:45.000000567+02:00".)
fzoneddatetime(date) Formats a date-time using ISO_ZONED_DATE_TIME, (for example "2000-01-12T23:34:45.000000567+02:00[UTC+02:00]")
flocaldate(date) Formats a date using ISO_LOCAL_DATE, (for example "2000-01-12".)
flocaldate(date, style) Formats a date using the specified style (FULL, LONG, MEDIUM, or SHORT.)
flocaltime(date) Formats a time using ISO_LOCAL_TIME, (for example "23:34:45.000000567".)
flocaltime(date, style) Formats a time using the specified style (FULL, LONG, MEDIUM, or SHORT)
flocaldatetime(date) Formats a date-time using ISO_LOCAL_DATE_TIME (for example "2000-01-12T23:34:45.000000567".)
flocaldatetime(date, style) Formats a date-time using the specified style for both date and time.
flocaldatetime(date, dateStyle, timeStyle) Formats a date-time using the specified styles for date and time.
fpattern(date, pattern) Formats a date-time using a custom pattern
fpattern(date, pattern, locale) Formats a date-time using a custom pattern and locale

Examples

${fdate(order.date)}
${flocaldate(invoice.date, 'FULL')}
${fpattern(meeting.time, 'EEEE, MMMM d, yyyy h:mm a')}
${fpattern(event.date, 'dd MMMM yyyy', 'fr')}

Using Functions in Templates

Use functions directly in expressions within your templates:

Order Date: ${fdate(order.date)}
Due Date: ${fpattern(order.dueDate, 'MMMM d, yyyy')}

You can also combine them with other SpEL features:

${order.status == 'OVERDUE' ? 'OVERDUE as of ' + fdate(order.dueDate) : 'Due on ' + fdate(order.dueDate)}

Custom Functions

You can add your own custom functions to extend the expression language. See the Custom Settings section for more information on how to register custom functions.

Examples of Custom Functions

Here are some examples of custom functions you might want to add:

// Add a function to get the current date
config.addCustomFunction("today", () -> LocalDate.now());

// Add a function to censor sensitive text
config.addCustomFunction("censor", String.class, input -> input.replace("f-word", "f**k"));

// Add a function to sum two numbers
config.addCustomFunction("add", Integer.class, Integer.class, (a, b) -> a + b);

// Add a function to format a date with a pattern and locale
config.addCustomFunction("format", LocalDate.class, String.class, String.class,
    (date, pattern, locale) -> DateTimeFormatter.ofPattern(pattern, locale).format(date));

Next Steps