Using $dateTool.toDate in a Velocity template does not return any date
Written By
Antonio Ortega
How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!
While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.
Legacy Article
You are viewing an article from our legacy "FastTrack"
publication program, made available for informational purposes. Articles
in this program were published without a requirement for independent
editing or verification and are provided"as is" without
guarantee.
Before using any information from this article, independently verify its
suitability for your situation and project.
Issue
- In a Velocity template, trying to get a date using dateTool.toDate does not work as expected when using a locale different from default one.
- As an example, if under those conditions we have a Velocity template with below code
#set ($dateStr = $dateTool.toDate( "EEE, dd MMM yyyy hh:mm:ss Z" , $reserved-article-display-date.data))
Date: $dateStr
- Instead of seeing the expected date, we'll just see how the name of variable $dateStr is printed.
Environment
Resolution
- Using $reserved-article-display-date.getData() helps to avoid this situation since it returns a String with default locale, so we just need, as well, to indicate to dateTool which is such default locale in order to properly deal with our date.
- Below code can solve the issue:
#set ($dateStr = $dateTool.toDate( "EEE, dd MMM yyyy hh:mm:ss Z" , $reserved-article-display-date.getData(), $localeUtil.getDefault())
Date: $dateStr
Did this article resolve your issue ?