Does the Timezone Needs to be Converted Manually When Fetching Database Entries Programmatically?


  • We changed the default timezone by doing the following:
1. Instance settings > localization > time zone > change to eastern daylight time
2. User Account settings > preferences > display settings > change to eastern daylight time
  • But now we have a question regarding custom code in this context - if we are fetching some entries from the database programmatically then are we required to convert the time manually or will it convert automatically?


  • 2023.Q4.8


  • The database always stores as GMT and it is up to the developer to display in the appropriate timezone. So to handle that, you will need to provide the conversion, but there are facilities in Liferay to assist with this. For example, for web content, to display the article display date, you could use code similar to:
Format dateFormat = FastDateFormatFactoryUtil.getDateTime(themeDisplay.getLocale(), themeDisplay.getTimeZone());
<p>Display Date: <%= dateFormat.format(article.getDisplayDate()) %></p>
  • This code uses the FastDateFormatFactoryUtil to get an instance of format that is set with the user's locale and user's timezone. When it is formatting the actual date, which is in GMT, the formatter converts to the user's local timezone and displays it in the manner expected. 

  • The portal leverages this kind of logic everywhere. The ThemeDisplay will be set up with the appropriate locale and timezone on a request-by-request basis and will be set to the user's timezone (if you have one assigned), falling back to the company timezone (if the user doesn't have one), so this technique outlined here will always give appropriate results. The FastDateFormatFactoryUtil has a number of different getDateTime() methods that take arguments to control formatting aspects (i.e. short dates vs long dates, etc) so there's lots of control available on how you want to control presentation.




¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0