7
4

1 回答 1

1

I don't think that any pre-defined constants would exist for year-and-month formatting in zh-CN locale, so you have to format it.

  new SimpleDateFormat("yyyy年M月").format(date)

As SimpleDateFormat says (highlighted by me):

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

So, it is safe to put chinese characters into the format string this way.

Since Java is Unicode, you can inline the characters in your source as long as you compile with unicode source character set. Otherwise you should read this String externally, e.g. from some Unicode property file or database.

于 2013-02-18T13:24:44.597 回答