Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 388
Steve McLeod Avatar asked May 04 '12 13:05

Steve McLeod


2 Answers

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.

like image 68
McDowell Avatar answered Sep 21 '22 20:09

McDowell


Perhaps use an ellipsis (...) instead of a dash?

like image 21
Matthew Watson Avatar answered Sep 17 '22 20:09

Matthew Watson