DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI

Trending

  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • The Update Problem REST Doesn't Solve
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Chat with Your Oracle Database: SQLcl MCP + GitHub Copilot
  1. DZone
  2. Coding
  3. Java
  4. Dataweave 2.4.0 Dates Module Functions

Dataweave 2.4.0 Dates Module Functions

This blog gives the details of the dw::core::Dates module functions which are released in DataWeave version 2.4.0 for Mule Version 4.4.

By 
Anand Joshi user avatar
Anand Joshi
·
Updated Oct. 18, 21 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
23.7K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave is a programming language designed for transforming data. It is the primary language of MuleSoft for data transformation and an expression language for components and connectors configuration.

Mulesoft released Dataweave 2.4.0 for Mule Version 4.4. The 2.4.0 version of DataWeave introduced many new features. In this post, we will see one of the newly introduced DataWeave modules.

dw::core::Dates is a new module that contains functions for creating and manipulating dates.

To use functions of this module, we need to import it in our Dataweave code as below:

import * from dw::core::Dates

dw::core::Dates has below functions:

1. atBeginningOfDay

 This function is used to change the Time value in the input DateTime to the beginning of the specified day which means hours, minutes, and seconds in the input change to 00:00:00.

Note: This function only accepts the input of type DateTime and LocalDateTime. 

Input: 

Java
 
{
"inputDate" : "2021-10-06T21:42:46"
}


DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
   outputDate :  atBeginningOfDay(payload.inputDate)   
}


Output: 

Java
 
{
  "outputDate": "2021-10-06T00:00:00Z"
}


2.  atBeginningOfHour 

This function is used to changes the Time value in the input DateTime/Time value to the beginning of the specified hour means minutes and seconds in the input change to 00:00.

Note: This function only accepts the input of type DateTime, LocalDateTime, LocalTime, Time.

Input: 

Java
 
{
"inputDate" : "2021-10-06T21:42:46"
}


DataWeave Expression: 

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
   outputDate :  atBeginningOfHour(payload.inputDate)   
}


Output: 

Java
 
{
  "outputDate": "2021-10-06T21:00:00Z"
}


3. atBeginningOfMonth

This function is used to change the Day value from the input DateTime value to the first day of the specified month. 

Note: This function only accepts the input of type DateTime, LocalDateTime, Date. If the input has a Time value then it also sets the Time value to 00:00:00.

Input: 

Java
 
{
"inputDateTime" : "2021-10-06T21:42:46",
"inputDate" :  "2021-10-06"
}


DataWeave Expression: 

JSON
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDateTime :  atBeginningOfMonth(payload.inputDateTime),
outputDate : atBeginningOfMonth(payload.inputDate),
}


Output: 

JSON
 
{
  "outputDateTime": "2021-10-01T00:00:00Z",
  "outputDate": "2021-10-01"
}


4. atBeginningOfWeek

This function is used to change the Day and Time values of the input DateTime value to the beginning of the first day of the week. The function treats Sunday as the first day of the week.

Note: This function only accepts the input of type DateTime, LocalDateTime, Date. If the input has a Time value then it sets the Time value to 00:00:00.

 Input: 

Java
 
{
"inputDateTime" : "2021-10-06T21:42:46",
"inputDate" :  "2021-10-06"
}


DataWeave Expression: 

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDateTime : atBeginningOfWeek(payload.inputDateTime),
outputDate : atBeginningOfWeek(payload.inputDate)
}


Output: 

Java
 
{
  "outputDateTime": "2021-10-03T00:00:00Z",
  "outputDate": "2021-10-03"
}


5. atBeginningOfYear

This function is used to change the input DateTime value to the first day of the year. 

Note: This function only accepts the input of type DateTime, LocalDateTime, Date. If the input has a Time value then it sets the Time value to 00:00:00.

Input: 

Java
 
{
"inputDateTime" : "2021-10-06T21:42:46",
"inputDate" :  "2021-10-06"
}


DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDateTime :  atBeginningOfYear(payload.inputDateTime),
outputDate : atBeginningOfYear(payload.inputDate)
}


Output:

Java
 
{
  "outputDateTime": "2021-01-01T00:00:00Z",
  "outputDate": "2021-01-01"
}


6. date

This function is used to create a Date value by passing values specified for the year, month, and day fields.

month -> Can have any value between 1 to 12

day -> Can have any value between 1 to 31

year -> Can have any value between 1000 to 9999

DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
   outputDate : date({year: 2021, month: 10, day: 11}) 
}


Output: 

Java
 
{
  "outputDate": "2021-10-11"
}


7. dateTime

This function is used to create a DateTime value by passing values specified for the year, month, day, hour, minutes, seconds, and timezone fields.

month -> Can have any value between 1 to 12

day -> Can have any value between 1 to 31

year -> Can have any value between 1000 to 9999

hour -> Can have any value between 0 to 23

minutes -> Can have any value between 0 to 59

seconds -> Can have any value between 0 to 59.99

timezone -> Should be passed in time offset format. E.g., |+05:30| , |-03:00| etc.

DataWeave Expression: 

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate: dateTime({year: 2021, month: 10, day: 11, hour: 10, minutes: 20, seconds: 30 , timeZone: |+05:00|}) 
}


Output: 

Java
 
{
  "outputDate": "2021-10-11T10:20:30+05:00"
}


8. localDateTime

This function is used to create a LocalDateTime value by passing values for the year, month, day, hour, minutes, and seconds fields.

month ->Can have any value between 1 to 12

day -> Can have any value between 1 to 31

year -> Can have any value between 1000 to 9999

hour -> Can have any value between 0 to 23

minutes -> Can have any value between 0 to 59

seconds -> Can have any value between 0 to 59.99

DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
 outputDate: localDateTime({year: 2021, month: 10, day: 11, hour: 10, minutes: 20, seconds: 30})
}


Output: 

Java
 
{
  "outputDate": "2021-10-11T10:20:30"
}


9. localTime

This function is used to create a LocalTime value by passing values for the hour,  minutes, and seconds fields. Valid values for these fields are as following: 

hour -> Can have any value between 0 to 23

minutes-> Can have any value between 0 to 59

seconds -> Can have any value between 0 to 59.99

DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
 outputDate: localTime({ hour: 10, minutes: 20, seconds: 30})
}


Output: 

Java
 
{
  "outputDate": "10:20:30"
}


10. time

This function is used to create a Time value by passing values for the hour, minutes seconds, and timezone fields. Valid values for these fields are as following : 

hour -> Can have any value between 0 to 23

minutes -> Can have any value between 0 to 59

seconds -> Can have any value between 0 to 59.99

timezone -> Should be passed in time offset format. E.g., |+05:30| or |-03:00|

DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate: time({ hour: 10, minutes: 20, seconds: 30 , timeZone: |+05:30|})
}


Output: 

Java
 
{
  "outputDate": "10:20:30+05:30"
}


11. today

This function gives today’s date as a Date type in response.

12. tomorrow

This function gives tomorrow’s date as a Date type in response.

13. yesterday

This function gives yesterday’s date as a Date type in response.

DataWeave Expression:

Java
 
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
"todayDate" : today(),
"tomorrowDate" : tomorrow(),
"yesterdayDate" : yesterday()
}


Output:

Java
 
{
  "todayDate": "2021-10-07",
  "tomorrowDate": "2021-10-08",
  "yesterdayDate": "2021-10-06"
}


This is how we can use dw::core::Date module functions. For more details please check Mulesoft official documentation here.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook