5

次のように日付範囲を示します。

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
}

オランダのユーザーの場合、「4-5-12-3-6-12」と表示されます。これは紛らわしいです。

ユーザーのロケールを考慮して日付範囲を表示する方法は何ですか?

4

4 に答える 4

5

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.

于 2012-05-04T13:29:30.067 に答える
1

おそらく、ダッシュの代わりに省略記号 (...) を使用しますか?

于 2012-05-04T13:12:39.690 に答える
1

少なくとも、値の周りにより多くのスペースを使用してください。

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

年を表す 4 桁は、私の知る限り、すべてのロケールで理解できます。

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

固定桁数は好みかもしれませんし、そうでないかもしれません:

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

2次から3次まではあまり改善が見られませんが、2次ではあります。私たちは皆、大きな数字よりも小さな数字と接触することが多く、小さな数字はほとんどありません。11、12、13 は頻繁に交差しますが、2011、2012、2013 を見ると、数年前から主に日付になっています。

于 2012-05-04T13:15:35.167 に答える
0

以下を使用できます。

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);

出力は次のとおりです。

SimpleDateFormat クラスによる日付: 6/14/08 1:15 PM

于 2012-05-04T13:25:46.777 に答える