What is a convenient way to show the I18N 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 the Netherlands they see "4-5-12-3-6-12". This is confusing.

How can I display date ranges that take user language into account?

+5
source share
4 answers

In a localized application, this separation will be decided by translators when they translate your resource packages:

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

Can be:

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

This can then be handled using MessageFormat . Something like 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 do not provide a full translation, this approach may be applicable for certain localizations.

+5
source

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

+1

, :

"4-5-12 - 3-6-12"

4 , afaik, :

"4-5-2012 - 3-6-2012"

a fixed number of digits may or may not be your taste:

"04-05-2012 - 03-06-2012"

I do not see much improvement from the 2nd to the 3rd form, but before the 2nd, there is. We are increasingly in contact with smaller than with large numbers, and their number is less. 11, 12 and 13 often cross your path, but if you see 2011, 2012 and 2013, then, starting from a few years, mostly in dates.

+1
source

You can use:

SimpleDateFormat sdf;

// Create new SimpleDateFormat object that returns default pattern.
sdf = new SimpleDateFormat();
String dateFormat = sdf.format(date);
System.out.println("Date by SimpleDateFormat class : " + dateFormat);

And the result:

SimpleDateFormat Date: 6/14/08 1:15 PM

+1
source

All Articles