简体   繁体   English

日期在Java中的不同语言环境中显示?

[英]Date display in different locales in Java?

在java中如何在不同的语言环境中显示日期(例如俄语)。

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 但是, 在jdk1.5中存在俄语日期格式的问题

The deal with Russian language is that month names have different suffix when they are presented stand-alone (ie 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 г." 所以,尽管三月是“Март”在俄语中,格式正确今天的日期是:“7Марта2007г.”

Let's see how JDK formats today's date: 7 Март 2007 г. 让我们来看看JDK如何格式化今天的日期:7月Март2007г。 Clearly wrong. 显然错了。

Use SimpleDateFormat constructor which takes locale. 使用带有语言环境的SimpleDateFormat构造函数。 You need to first check if JDK supports the locale you are looking for, if not then you need to implement that. 您需要先检查JDK是否支持您要查找的语言环境,否则您需要实现它。

Use the java.text.DateFormat class, you can construct that's configured to a specific Locale. 使用java.text.DateFormat类,您可以构造配置为特定Locale的类。

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

The DateFormat class can help you. DateFormat类可以帮助您。 As explained in the Javadoc: 正如Javadoc中所解释的那样:

To format a date for a different Locale, specify it in the call to getDateInstance(). 要格式化其他区域设置的日期,请在对getDateInstance()的调用中指定它。

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

So you just need to adapt this code by using the adequate Locale. 因此,您只需使用适当的Locale来调整此代码。

使用带有适当时区和语言环境的java.util.Calendar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM