如何在 Java 中按语言环境格式化日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:44:23  来源:igfitidea点击:

How can I format date by locale in Java?

javadateformattinglocale

提问by newbie

I need to format date to app that has many languages, what is best way to format date, because every country has different kind of date formatting, so is it possible to format date by locale?

我需要将日期格式化为具有多种语言的应用程序,格式化日期的最佳方法是什么,因为每个国家/地区都有不同类型的日期格式,那么是否可以按区域设置格式化日期?

采纳答案by Bozho

Yes, using DateFormat.getDateInstance(int style, Locale aLocale) This displays the current date in a locale-specific way.

是的,使用DateFormat.getDateInstance(int style, Locale aLocale)这以特定于语言环境的方式显示当前日期。

So, for example:

因此,例如:

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, yourLocale);
String formattedDate = df.format(yourDate);

See the docs for the exact meaning of the style parameter (SHORT, MEDIUM, etc)

请参阅该文档的样式参数的确切含义(SHORTMEDIUM,等)

回答by laura

SimpleDateFormat has a constructor which takes the locale, have you tried that?

SimpleDateFormat 有一个接受语言环境的构造函数,你试过了吗?

http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Something like new SimpleDateFormat("your-pattern-here", Locale.getDefault());

就像是 new SimpleDateFormat("your-pattern-here", Locale.getDefault());

回答by Mihai Nita

Take a look at java.text.DateFormat. Easier to use (with a bit less power) is the derived class, java.text.SimpleDateFormat

看看java.text.DateFormat。派生类java.text.SimpleDateFormat更易于使用(功能更少)

And here is a good intro to Java internationalization: http://java.sun.com/docs/books/tutorial/i18n/index.html(the "Formatting" section addressing your problem, and more).

这是 Java 国际化的一个很好的介绍:http: //java.sun.com/docs/books/tutorial/i18n/index.html(解决您的问题的“格式”部分等等)。

回答by Baztoune

I agree with Laura and the SimpleDateFormat which is the best way to manage Dates in java. You can set the pattern and the locale. Plus you can have a look at this wikipedia article about Date in the world-there are not so many different ways to use it; typically USA / China / rest of the world -

我同意 Laura 和 SimpleDateFormat,这是在 Java 中管理日期的最佳方式。您可以设置模式和语言环境。另外,您可以查看这篇关于世界上 Date 的维基百科文章- 使用它的方法并不多;通常美国/中国/世界其他地区 -

回答by Basil Bourque

Joda-Time

乔达时间

Using the Joda-Time2.4 library. The DateTimeFormatclass is a factory of DateTimeFormatterformatters. That class offers a forStylemethod to access formatters appropriate to a Locale.

使用Joda-Time2.4 库。这个DateTimeFormat类是一个DateTimeFormatter格式化工厂。该类提供了一种forStyle方法来访问适合于Locale.

DateTimeFormatter formatter = DateTimeFormat.forStyle( "MM" ).withLocale( Java.util.Locale.CANADA_FRENCH );
String output = formatter.print( DateTime.now( DateTimeZone.forID( "America/Montreal" ) ) );

The argument with two letters specifies a format for the date portion and the time portion. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be ommitted by specifying a style character '-' HYPHEN.

带有两个字母的参数指定日期部分和时间部分的格式。指定字符 'S' 代表短款,'M' 代表中号,'L' 代表长款,'F' 代表全款。可以通过指定样式字符“-”连字符来省略日期或时间。

Note that we specified both a Locale and a time zone. Some people confuse the two.

请注意,我们同时指定了区域设置和时区。有些人将两者混淆。

  • A time zone is an offset from UTC anda set of rules for Daylight Saving Time and other anomalies along with their historical changes.
  • A Locale is a human language such as Fran?ais, plus a country code such as Canada that represents cultural practices including formatting of date-time strings.
  • 时区是与 UTC 的偏移量以及一组针对夏令时和其他异常及其历史变化的规则。
  • Locale 是一种人类语言,例如 Fran?ais,加上代表文化习俗的国家代码(例如 Canada),包括日期时间字符串的格式设置。

We need all those pieces to properly generate a string representation of a date-time value.

我们需要所有这些部分来正确生成日期时间值的字符串表示。