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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Using Java Stream Gatherers To Improve Stateful Operations
  • How to Merge HTML Documents in Java

Trending

  • Next-Gen IoT Performance Depends on Advanced Power Management ICs
  • Optimizing Software Performance for High-Impact Asset Management Systems
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Assessing Bias in AI Chatbot Responses
  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.2K 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

  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Using Java Stream Gatherers To Improve Stateful Operations
  • How to Merge HTML Documents in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!