-
Notifications
You must be signed in to change notification settings - Fork 33
Description
About the doc:
- Format Dates and Times | MuleSoft Documentation
- docs-dataweave/modules/ROOT/pages/dataweave-cookbook-format-dates.adoc at v2.5 · mulesoft/docs-dataweave (github.com)
Doesn't have the format YYYY that can confused with yyyy
ℹ️ There is some context about it using two letters patterns, but not with four.
//y is for use with the era (BCE or CE). Generally, use u, instead.
"year-y" : myDateTime as String { format: "y"},
"year-yy" : myDateTime as String { format: "yy"}, // Understand "Y" and "YY" thoroughly before using it.
"weekBasedYear-YY" : myDateTime as String { format: "YY"},
"weekBasedYear-Y" : myDateTime as String { format: "Y"},Explanation:
In Java, yyyy and YYYY are used in date formatting and they have different meanings:
The week-based year changes for the first Thursday of the new year, or if the year starts on a Friday and is a leap year. So, for example, if December 31 falls on a Monday, Tuesday, or Wednesday, then it will be part of week 1 of the following year. Similarly, if January 1 falls on a Friday, Saturday, or Sunday, it will be part of the last week of the previous year.
Here’s an example to illustrate this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd"); System.out.println(formatter.format(LocalDate.of(2022, 12, 31))); // Prints: 2023/12/31 formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); System.out.println(formatter.format(LocalDate.of(2022, 12, 31))); // Prints: 2022/12/31In the above example, December 31, 2022, is considered as part of the first week of 2023 according to the week-based year (YYYY), but it’s still in 2022 according to the calendar year (yyyy). So, when formatting dates in Java, it’s important to choose the correct pattern based on your requirements
DataWeave
%dw 2.0
output application/dw
---
{
"YYYY": |2023-12-31| as String { format: "dd/MM/YYYY" },
"yyyy": |2023-12-31| as String { format: "dd/MM/yyyy" },
}Result:
{
YYYY: "31/12/2024" as String {format: "dd/MM/YYYY"},
yyyy: "31/12/2023" as String {format: "dd/MM/yyyy"}
}