Julian dates are great for storage efficiency—computers love them—but they are terrible for human readability. In this guide, we’re going to tackle a common data transformation task: taking a Julian number and turning it into a clean, standard date format (like yyyy-MM-dd) using DataWeave 2.0.
We will keep the logic robust to handle various input lengths, ensuring your application doesn't crash just because a date is missing a leading zero.
1. The Setup: Script Header
First things first. We need to tell DataWeave we are outputting JSON and that we need the Strings module. This module provides the helper functions (like leftPad) we need to format our numbers correctly.
%dw 2.0
output application/json
import * from dw::core::Strings
2. The Logic: Creating the Conversion Function
Here is where the magic happens. We are going to define a function called julianToStandard. Think of this function as a sorting machine that looks at the size of the package (the number length) to decide how to unpack it.
The function logic follows a strict "Fact-Preservation" protocol. It checks the length of the input string and calculates the year relative to 1900.
How the Logic Flows:
- Length > 5: It assumes a full format, pads it to 6 digits, and slices it to find the day and year.
- Length > 4: It extracts the day from the end and the year from the first two digits.
- Length > 3: It handles shorter dates by defaulting the first digit or calculating slightly differently.
- Length >= 1: It assumes the year is strictly 1900.
- Catch-all: If the input doesn't match these rules, it returns
0.
The Calculation Strategy
Once the Year and Day count are extracted, the script performs date arithmetic:
- It starts at January 1st of the calculated Year.
- It adds the number of days (minus 1) to that start date.
Here is the complete logic block:
// Function to convert Julian date to standard date format
fun julianToStandard(julianDate: Number) = do {
var myDate = if (sizeOf(julianDate splitBy '') > 5)
{
date: leftPad(julianDate, 6, 0),
day: leftPad(julianDate, 6, 0)[3 to -1],
year: 1900 + leftPad(julianDate, 6, 0)[0 to 2]
}
else if (!isEmpty(julianDate) and !(julianDate ~= 0) and sizeOf(julianDate splitBy '') > 4)
{
date: julianDate,
day: julianDate[2 to -1],
year: 1900 + julianDate[0 to 1]
}
else if (!isEmpty(julianDate) and !(julianDate ~= 0) and sizeOf(julianDate splitBy '') > 3)
{
date: julianDate,
day: julianDate[1 to -1],
year: 1900 + (julianDate[0] default 0)
}
else if (!isEmpty(julianDate) and !(julianDate ~= 0) and sizeOf(julianDate splitBy '') >= 1)
{
date: julianDate,
day: julianDate[0 to -1],
year: 1900
}
else
0
---
if (myDate ~= 0)
0
else
(
(
("0101" ++ myDate.year) as Date {format: "ddMMyyyy"}
) + ("P$(myDate.day - 1)D" as Period)
) as Date {format: "yyyy-MM-dd"}
}
3. Putting It All Together
Now that the function is defined, we simply call it. In this example, we test it with a specific variable (99211), which represents the 211th day of the year 1999.
Below is the full, executable script you can copy directly into your MuleSoft Transform Message component.
%dw 2.0
import * from dw::core::Strings
output application/json
// Function to convert Julian date to standard date format
fun julianToStandard(julianDate: Number) = do {
var myDate = if (sizeOf(julianDate splitBy '') > 5)
{
date: leftPad(julianDate, 6, 0),
day: leftPad(julianDate, 6, 0)[3 to -1],
year: 1900 + leftPad(julianDate, 6, 0)[0 to 2]
}
else if (!isEmpty(julianDate) and !(julianDate ~= 0) and sizeOf(julianDate splitBy '') > 4)
{
date: julianDate,
day: julianDate[2 to -1],
year: 1900 + julianDate[0 to 1]
}
else if (!isEmpty(julianDate) and !(julianDate ~= 0) and sizeOf(julianDate splitBy '') > 3)
{
date: julianDate,
day: julianDate[1 to -1],
year: 1900 + (julianDate[0] default 0)
}
else if (!isEmpty(julianDate) and !(julianDate ~= 0) and sizeOf(julianDate splitBy '') >= 1)
{
date: julianDate,
day: julianDate[0 to -1],
year: 1900
}
else
0
---
if (myDate ~= 0)
0
else
(
(
("0101" ++ myDate.year) as Date {format: "ddMMyyyy"}
) + ("P$(myDate.day - 1)D" as Period)
) as Date {format: "yyyy-MM-dd"}
}
var mydate = "99211"
---
julianToStandard(mydate as Number)
Conclusion
And there you have it! This script provides a reliable way to normalize those tricky Julian dates into something your database or downstream API can actually understand. By leveraging string manipulation combined with date arithmetic, we ensure accuracy regardless of the input format's slight variations.