从java SimpleDataFormat获取模式字符串

从java SimpleDataFormat获取模式字符串,java,simpledateformat,Java,Simpledateformat,我有一个SimpleDataFormat对象,可以从一些国际化实用程序中检索。解析日期很好,但我希望能够向我的用户显示格式提示,如“MM/dd/yyyy”。有没有办法从SimpleDateFormat对象获取格式化模式?使用toPattern()或toLocalizedPattern()方法。 返回一个模式字符串,该字符串描述 这个日期格式 如果您只需要获取给定区域设置的模式字符串,那么以下内容对我很有用: /* Obtain the time format per current locale

我有一个SimpleDataFormat对象,可以从一些国际化实用程序中检索。解析日期很好,但我希望能够向我的用户显示格式提示,如“MM/dd/yyyy”。有没有办法从SimpleDateFormat对象获取格式化模式?

使用
toPattern()
toLocalizedPattern()
方法。

返回一个模式字符串,该字符串描述 这个日期格式


如果您只需要获取给定区域设置的模式字符串,那么以下内容对我很有用:

/* 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在这里很关键。

谢谢,我想我看得不够近。你能告诉我,如何从这个字符串中获取日期或时间模式吗?我可以使用诸如compare和contains之类的字符串方法,但是还有比这更优雅的方法吗?
/* 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();
}