Location based Date Time Formatting

Learn to display the date and time information to the end-user in a location-sensitive manner, based on the user’s timezone. We will show the zone-specific dates in the default locale as well as the custom locales.

1. Displaying Locale Formatted Date and Time

To display information in a locale-sensitive manner, we must follow the two steps:

  • Get the current locale of the user. If locale information is not represent the use a default locale.
  • Use the locale information to format the date and time message.

Let us discuss each step in brief.

It is important to understand the Locale specific formatting changes only the display structure and language of the information, it does not modify the date and time values according to the timezone offsets.

2. Getting the User Locale

Note that fetching the current locale information varies from application to application and framework to framework.

For example, we can use the AcceptHeaderLocaleResolver in a Spring web application that uses the primary locale specified in the “accept-language” header of the HTTP request (the locale sent by the client browser, usually that of the client’s OS).

@Bean
public LocalResolver localeResolver() {
    AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
    localeResolver.setSupportedLocales(Arrays.asList(new Locale("fa"), new Locale("en")));
    localeResolver.setDefaultLocale(new Locale("fa"));
    return localeResolver;
}

And to get the locale-specific language tag, use LocaleContextHolder.getLocale() method call.

3. Format Date Time based on Locale

When we have the locale information or the language tag, we can use this information to create a Locale-specific DateTimeFormatter instance that can format any type of date or time information in Java.

In the given an example, we are displaying the date for the Chinese locale:

ZonedDateTime now = ZonedDateTime.now();
    
DateTimeFormatter format = DateTimeFormatter
   .ofLocalizedDateTime(FormatStyle.MEDIUM)
   .withLocale(Locale.forLanguageTag("zh"));
   //.withLocale(LocaleContextHolder.getLocale());

String formmatedDate = format.format(now);
System.out.println(formmatedDate);

Program output.

2022年2月20日 下午11:10:08

The same code for Spanish locale Locale.forLanguageTag("es") will give the output.

20 feb 2022 23:11:03

4. Conclusion

This short tutorial taught us to display the date and time information based on the user locale. As mentioned earlier, locale-based formatting only changes information display and does not modify the date values based on the timezone rules.

If you wish to change the date and time values as well for a given timezone then use DateTimeFormatter.withZone(zoneId).

Happy Learning !!

Sourcecode Download

Comments

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.