开发者

How to Configure Pattern for DateFormat's getDateInstance()

Is there any away to configure the pattern for String values returned by DateFormat's getDateInstance(), for each style - SHORT, MEDIUM, LONG, and FULL?

For example, DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US) w开发者_运维百科ill always return a value something like Jan 12, 1952. How can I have this return 1952, Jan 12?


You can customize your own date format using SimpleDateFormat.

    String formatted = new SimpleDateFormat("yyyy, MMM dd").format(new Date(52, 0, 12)); 
    System.out.println(formatted); //prints "1952, Jan 12" 

Edit

Ok if you are trying to override for just some specific locales, you can configure your own DateFormatProvider:

class CustomDateFormatProvider extends DateFormatProvider {
    public DateFormat getDateInstance(int style, Locale locale) {
        if ( style == DateFormat.MEDIUM && Locale.US.equals(locale) ) {
            return new SimpleDateFormat("yyyy, MMM dd");
        }
        //else do default behaviour
    }

    //...
}

This can be installed using the Java Extension Mechanism. See LocaleServiceProvider.

If this isn't what you're looking for you should add some actual requirements to your question. I addressed the question you asked, but it seems there's a question you didn't ask for which you're still waiting for an answer.

But obviously this will only alter ones that you specifically override.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜