いくつかの国際化ユーティリティから取得したSimpleDateFormatオブジェクトがあります。日付の解析はすべて問題なく機能しますが、「MM / dd/yyyy」のような書式設定のヒントをユーザーに表示できるようにしたいと思います。SimpleDateFormatオブジェクトからフォーマットパターンを取得する方法はありますか?
26740 次
4 に答える
36
この日付形式を説明するパターン文字列を返します。
于 2010-05-03T21:51:21.337 に答える
6
特定のロケールのパターン文字列を取得する必要がある場合は、次のようにします。
/* Obtain the time format per current locale */
public String getTimeFormat(int longfmt) {
Locale loc = Locale.getDefault();
int jlfmt =
(longfmt == 1)?java.text.SimpleDateFormat.LONG:java.text.SimpleDateFormat.SHORT;
SimpleDateFormat sdf =
(SimpleDateFormat)SimpleDateFormat.getTimeInstance(jlfmt, loc);
return sdf.toLocalizedPattern();
}
/* Obtain the date format per current locale */
public String getDateFormat(int longfmt) {
Locale loc = Locale.getDefault();
int jlfmt =
(longfmt == 1)?java.text.SimpleDateFormat.LONG:java.text.SimpleDateFormat.SHORT;
SimpleDateFormat sdf =
(SimpleDateFormat)SimpleDateFormat.getDateInstance(jlfmt, loc);
return sdf.toLocalizedPattern();
}
ここでは、特定のロケールの getDateInstance と getTimeInstance が重要です。
于 2012-01-08T05:10:33.350 に答える
5
使用toPattern()
またはtoLocalizedPattern()
方法。
于 2010-05-03T21:50:15.653 に答える
4
import java.text.SimpleDateFormat;
public class Sdf {
public static void main( String [] args ) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String patternToUse = sdf.toPattern();
System.out.println( patternToUse );
}
}
于 2010-05-03T21:55:07.653 に答える