0

I wanted to have my app display

September 2014

in English locale and

2014年9月

in Chinese locale.

Are there any way in Java (especially, Java 1.6 in Android) can do this?

I am interesting in the "natural" way to display the year and the month in a specific culture. So English and Chinese cultures are just an example, and the solution shall work for all system supported cultures.

For the example above, if I have something like 9月 2014 I am sure every Chinese speaker will know what it means but it is just looked very odd in that culture. On the other hand, 2014年九月 is acceptable.

Attempts

  1. The pattern for SimpleDateFormat need you to specify the order of year and month field. Since this is vary in different languages this is not going to solve it.

  2. If you try to bend the rule to allow displaying 2014, September for English and 2014年, 9月 for Chinese (this is acceptable, but 2014, 9月 is still odd) and consider Calendar.getDisplayName, unfortunately it returns null for Calendar.Year.

Earth Engine
  • 10,048
  • 5
  • 48
  • 78
  • Do you need a solution for any locale, or are you just looking for a solution for English and Chinese? – jarnbjo Sep 20 '14 at 14:58
  • possible duplicate of [How can I format date by locale in Java?](http://stackoverflow.com/questions/1951849/how-can-i-format-date-by-locale-in-java). And [this](http://stackoverflow.com/q/1661325/642706). – Basil Bourque Sep 20 '14 at 17:20
  • Tip: [Joda-Time](http://www.joda.org/joda-time/). – Basil Bourque Sep 20 '14 at 17:23

1 Answers1

0

Maybe that help, but it's not tested under android:

public static void main(String[] args) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        Date d = sdf.parse("01.09.2014");

        // get the formatter for a specific country/language
        sdf = (SimpleDateFormat) DateFormat.getDateInstance(
                DateFormat.LONG, Locale.CHINESE);

        System.out.println(sdf.toLocalizedPattern());
        System.out.println(sdf.format(d));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
DirkNM
  • 2,614
  • 15
  • 21