mads
mads

Reputation:

Date display in different locales in Java?

In java how do I display dates in different locales (for e.g. Russian).

Upvotes: 7

Views: 7895

Answers (5)

VonC
VonC

Reputation: 1323183

Something like:

Locale locale = new Locale("ru","RU");
DateFormat full = DateFormat.getDateInstance(DateFormat.LONG, locale);
out.println(full.format(new Date()));

Should do the trick. However, there was a problem of Russian Date formatting in jdk1.5

The deal with Russian language is that month names have different suffix when they are presented stand-alone (i.e. in a list or something) and yet another one when they are part of a formatted date. So, even though March is "Март" in Russian, correctly formatted today's date would be: "7 Марта 2007 г."

Let's see how JDK formats today's date: 7 Март 2007 г. Clearly wrong.

Upvotes: 14

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Use SimpleDateFormat constructor which takes locale. You need to first check if JDK supports the locale you are looking for, if not then you need to implement that.

Upvotes: 5

Romain Linsolas
Romain Linsolas

Reputation: 81577

The DateFormat class can help you. As explained in the Javadoc:

To format a date for a different Locale, specify it in the call to getDateInstance().

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

So you just need to adapt this code by using the adequate Locale.

Upvotes: 3

Tom
Tom

Reputation: 44821

Use the java.text.DateFormat class, you can construct that's configured to a specific Locale.

DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, theLocaleYouWant);
String text = format.format(new Date());
System.out.println(text);

Upvotes: 4

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64026

Use a java.util.Calendar with an appropriate time zone and locale.

Upvotes: 0

Related Questions