News Ticker

DataWeave: Format date locale

Format Dates in DataWeave

Main points:

  • Dates can be formatted to a locale
  • The locale can be different from the system locale
  • Use Java locale properties
  • Coerce String to Date then to formatted String

Format Date and String

You want to format a date, such as 12/10/2010, into a local format, such as 12, octubre, 2010 or vice versa.

Coerce String to Date

The process is to format the date, which is received as a String, to a Date then back to a String, but with a specified Date format, as shown in figure 1.

"12/10/2010" as Date {format: "dd/MM/yyyy"}

Figure 1: Coerce String to Date format

Coerce Date to Formatted String

The date is then formatted as a String by providing an output format and locale property, as shown in figure 2.

 "12/10/2010" 
    as Date {format: "dd/MM/yyyy"} 
    as String {format: "dd, MMMM, yyyy", locale: "ES"}

Figure 2: Format Date as String to Spanish locale

The output is the date formatted to the Spanish locale: 12, octubre, 2010. The locale property is defined in the ISO 639 language codes.

The reverse operation is also possible, as shown in figure 3.

"12, octubre, 2010" 
    as Date {format: "dd, MMMM, yyyy", locale: "es"} 
    as String {format: "dd/MM/yyyy"}

Figure 3: Reformat Date

Conclusion

To format a date with a locale it’s necessary to first coerce to a Date format then format it to the desired Date format and specify a locale.

Leave a Reply

%d bloggers like this: