Using $dateTool.toDate in a Velocity template does not return any date

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

  • Liferay Portal 6.2

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

Additional Information

 

这篇文章有帮助吗?
0 人中有 0 人觉得有帮助