-
dateTime
-
nanoTime
Mule Expression Language Date and Time Functions
Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version. |
This reference page provides lists and brief explanations of the date and time functions in MEL. For explanatory information, see Mule Expression Language, MEL Basic Syntax, and the MEL examples.
Assumptions
This document assumes you are familiar with Mule Expression Language (MEL). Further, this document assumes you are using Mule 3.4.0 or later.
Obtaining the Server Time
You can obtain the current date and time of the server using the context object server and one of the following fields:
-
`dateTime
-
`nanoTime
For example, the following expression returns the date and time of the server: #[server.dateTime]
You can further specify the data you wish to extract using the following quantifiers:
-
server.dateTime.getMilliSeconds()
-
seconds
-
minutes
-
hours
-
dayOfWeek
-
dayOfMonth
-
dayOfYear
-
weekOfMonth
-
weekOfYear
-
month
-
year
The following table describes operations you can perform with dateTime. The sections below offer details on how to use the operations in MEL expressions.
Action | Operations |
---|---|
create a new DateTime instance |
|
|
|
|
|
access timezones |
|
|
|
|
Obtain DateTime
ContextObject.Field | Description | Return Type | Example |
---|---|---|---|
server.dateTime |
Current system date and time in a DateTime object that simplifies the parsing/formatting and very basic manipulation of dates via the Mule expression language. |
org.mule.el.datetime.DateTime |
|
server.nanoTime() |
Current system time in nanoseconds. Equivalent to System.nanoTime() and has the same limitations, that is, should be used only to measure elapsed time, etc. |
int |
|
Access Partial DateTime Information
Qualifier | Description | Return Type | Example |
---|---|---|---|
milliSeconds |
Returns the number of milliseconds in the current second. |
long |
|
seconds |
Returns the number of seconds passed in the current minute (0 to 59). |
int |
|
minutes |
Returns the number of minutes passed in the current hour (0 to 59). |
int |
|
hours |
Returns the number of hours passed in the current day (0 - 24). |
int |
|
dayOfWeek |
Returns one of the following integer values: Calendar.SUNDAY = 1, Calendar.MONDAY = 2, Calendar.TUESDAY = 3, Calendar.WEDNESDAY = 4, Calendar.THURSDAY = 5, Calendar.FRIDAY = 6, Calendar.SATURDAY = 7; Equivalent to calendar.get(Calendar.DAY_OF_WEEK) |
int |
|
dayOfMonth |
Returns the day of the month (1 to 31). |
int |
|
dayOfYear |
Returns the day of the year (1 to 366). |
int |
|
weekOfMonth |
Returns the week of the month (1 to 5). |
int |
|
weekOfYear |
Returns the week of the year (1 - 53) |
int |
|
month |
Returns the month of the year (1 - 12) |
int |
|
year |
Returns the the year (for example, 2013). |
int |
|
Create New DateTime Instance
Function | Description | Example | ||||||
---|---|---|---|---|---|---|---|---|
DateTime() |
Constructs a DateTime with the current time and the time zone and locale of the server. |
|
||||||
DateTime(calendar, locale) |
Constructs a DateTime with the calendar and locale specified.
|
|
||||||
DateTime(calendar) |
Constructs a DateTime with the calendar specified and the locale of the server.
|
|
||||||
DateTime(calendar) |
Constructs a DateTime with the calendar specified and the locale of the server.
|
|
||||||
DateTime(date) |
Constructs a DateTime with the specified date and the locale and time zone of the server.
|
|
||||||
DateTIme(iso8601String) |
Construct a DateTime using the specified iso8601 date.
|
|
||||||
DateTime(String dateString, String format) |
Constructs a DateTime used a string containing a date time in the specified format. The format should be SimpleDateFormat compatible.
Throws exception: ParseException |
|
Compute Relative Dates
Functions | Description | Return Type |
---|---|---|
plusMilliSeconds(int add) |
Returns the DateTime with the given amount of milliseconds added (or subtracted if it is a negative value). |
DateTime |
plusSeconds(int add) |
Returns the DateTime with the given amount of seconds added (or subtracted if it is a negative value). |
DateTime |
plusMinutes(int add) |
Returns the DateTime with the given amount of minutes added (or subtracted if it is a negative value). |
DateTime |
plusHours(int add) |
Returns the DateTime with the given amount of hours added (or subtracted if it is a negative value). |
DateTime |
plusDays(int add) |
Returns the DateTime with the given amount of days added (or subtracted if it is a negative value). |
DateTime |
plusWeeks(int add) |
Returns the DateTime with the given amount of weeks added (or subtracted if it is a negative value). |
DateTime |
plusMonths(int add) |
Returns the DateTime with the given amount of months added (or subtracted if it is a negative value). |
DateTime |
plusYears(int add) |
Returns the DateTime with the given amount of years added (or subtracted if it is a negative value). |
DateTime |
Example
#[payload = 'Two days ago it was the ' + server.dateTime.plusDays(-2).dayOfWeek + 'st day of the week';]
Compare
Function | Description | Return Type | Example |
---|---|---|---|
isBefore(ortherInstant) |
Returns whether this Calendar represents a time before the instant represented by the specified argument. |
boolean |
|
isAfter(otherInstant) |
Returns whether this Calendar represents a time after the instant represented by the specified argument. |
boolean |
|
Access Timezones
Function | Description | Return Type | Example | ||||
---|---|---|---|---|---|---|---|
withTimeZone(timezone); |
Changes the current DateTime to match a defined timezone. Effectively changing the dateTime and the timezone of the instance.
|
DateTime |
|
||||
changeTimeZone(timezone) |
Changes the timezone of the instance. Effectively changing only the timezone of the instance.
|
DateTime |
|
||||
timeZone |
Returns the current TimeZone of the dateTime instance. |
string |
|
||||
withLocale(localeAsString); |
This method takes the string format of a locale and creates the locale object from it.
|
DateTime |
|
Format Strings
Function | Description | Return Type | Example | ||||
---|---|---|---|---|---|---|---|
format() |
Formats the instance in a string with the ISO8601 date time format. |
string |
|
||||
format(String pattern) |
Formats the instance in a specific format.
|
string |
|
When writing in Studio’s XML editor, you cannot use double quotes to express String literals, because MEL expressions already appear enclosed in double quotes in configuration files. Instead, you can either:
If you’re writing on Studio’s visual editor, double quotes will be transformed into escaped quotes` ("`) in the XML view. |
Transform
Function | Description | Return Type | Example |
---|---|---|---|
toCalendar() |
Returns a Java Calendar representation of the dateTime instance. |
Calendar |
|
toDate() |
Returns a Java Date representation of the datetime instance. |
java.util.Date |
|
toXMLCalendar() |
Returns a XMLCalendar representation of the datetime instance. Throws: DatatypeConfigurationException |
XMLGregorianCalendar |
|
See Also
-
Learn more about the Mule Expression Language (MEL).