There are a number of ways to work with dates in mirth. The simplest is using DateUtil, which is provided with Mirth, along with Date objects or strings. You can also use java, string functions, and regex to work with dates. For most cases, DateUtil should be a good solution.
DateUtil provides you with four functions to use:
- Date getDate(String pattern, String date)
- String formatDate(String pattern, Date date)
- String getCurrentDate(String pattern)
- String convertDate(String inPattern, String outPattern, String date)
The first, getDate, is used to convert strings that contain dates into date objects. The first argument is the formatting, the second is the date itself.
The second, formatDate, converts date objects into strings, with whatever formatting you specify. The arguments are the same as the previous function, although the date is an actual Date object.
The third, getCurrentDate, returns the current time in the system time zone in a string, with the formatting specified in the first argument.
The final one, convertDate, is a combination of the first two. It takes a string date and converts it to a different format, as do the other examples on this page. The first argument is the format the date is currently in, the second is the desired output format, and the third is the date itself. An example of this is provided below.
If you want to use Java to format dates, you can use code similar to this:
The first simpleDateFormat takes a string that defines the incoming format of the date from HL7 and creates a JAVA date object. The next formatter defines the output format we want.
You can also use simple string functions. Since they have set formats, substr calls can process dates without a need for Java calls.
Regular expressions are another viable way to work with dates,and there is a performance benefit to using them over string functions.
In case you're unfamiliar with date formats, here is a list of characters you can use in your format strings and what they represent (As you can probably see from the multiple cases in the characters below, these are case sensitive).
- G - Era Text (e.g. AD)
- y - Year
- M - Month
- w - Week (in the year)
- W - Week (in the month)
- D - Day (in the year)
- d - Day (in the month)
- F - Day (of the week; numeric)
- E - Day (of the week; word)
- a - AM/PM
- H - Hour (0-23)
- k - Hour (1-24)
- K - Hour (0-11)
- h - Hour (1-12)
- m - Minute
- s - Second
- S - Millisecond
- z - Time zone
- Z - Time zone (different formatting)