What's a I18N-friendly way to show a date range?

internationalization, java, jodatime

Solution

In a localized application, such separation would be decided by the translators when they translate your resource bundles:

#foo.properties
#comment: a date range
dateRange={0,date}-{1,date}

Might become:

#foo_en.properties
dateRange={0,date} to {1,date}

This can then be handled using the MessageFormat type. Something of the form:

//untested code
MessageFormat mf = new MessageFormat(formatString, locale);
//java.util.Date instances
Object[] range = {date1, date2};
String result = mf.format(range);

Even if you're not providing full translations, this approach might be applicable for certain localizations.

Problem

I show date ranges as follows: ``` public static void main(String[] args) { Locale.setDefault(new Locale("nl", "NL")); DateTime d1 = new DateTime(); DateTime d2 = new DateTime().plusDays(30); final String s1 = d1.toString(DateTimeFormat.shortDate()); final String s2 = d2.toString(DateTimeFormat.shortDate()); System.out.println(s1 + "-" + s2); // shows "4/05/12-3/06/12" for en_AU System.out.println(s1 + "-" + s2); // shows "4-5-12-3-6-12" for nl_NL } ``` For users in Holland, they see "4-5-12-3-6-12". This is confusing. What's a way to show date ranges that takes the user's locale into account?

Original source