语言环境的 Java 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1339351/
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-12 08:24:16  来源:igfitidea点击:

Java Date Format for Locale

javadateformatlocale

提问by

How can I find the DateFormatfor a given Locale?

我怎样才能找到DateFormat给定的Locale

回答by oxbow_lakes

DateFormat.getDateInstance(int,Locale)

For example:

例如:

import static java.text.DateFormat.*;

DateFormat f = getDateInstance(SHORT, Locale.ENGLISH);

Then you can use this object to format Dates:

然后你可以使用这个对象来格式化Dates:

String d = f.format(new Date());

If you actually want to know the underlying pattern (e.g. yyyy-MMM-dd) then, as you'll get a SimpleDateFormatobject back:

如果你真的想知道底层模式(例如yyyy-MMM-dd),那么你会得到一个SimpleDateFormat对象:

SimpleDateFormat sf = (SimpleDateFormat) f;
String p1 = sf.toPattern();
String p2 = sf.toLocalizedPattern();

回答by Basil Bourque

tl;dr

tl;博士

DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
                 .withLocale( Locale.CANADA_FRENCH );

java.time

时间

The original date-time classes are now legacy and have been supplanted by the java.time classes.

原来的日期时间类现在是遗留的,已经被 java.time 类所取代。

The DateTimeFormatterhas a simple way to localize automatically by Localewhen generating a String to represent the date-time value. Specify a FormatStyleto indicate length of output (abbreviated or not).

当生成一个字符串来表示日期时间值时,DateTimeFormatter有一个简单的方法来自动本地化Locale。指定 aFormatStyle以指示输出的长度(缩写与否)。

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL );
f = f.withLocale( Locale.CANADA_FRENCH );

Get the current moment. Notice that Localeand time zone have nothing to do with each other. One determines presentation, the other adjusts into a particular wall-clock time. So you can have a time zone in New Zealand with a Localeof Japanese, or in this case a time zone in India with presentation formatted for a Québécois person to read.

获取当前时刻。请注意,Locale和时区彼此无关。一个决定演示,另一个调整到特定的挂钟时间。因此,您可以在新西兰使用Locale日语时区,或者在这种情况下使用印度时区,其演示文稿格式为魁北克人阅读。

ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = ZonedDateTime.now( z );

Generate a String using that localizing formatter object.

使用该本地化格式化程序对象生成一个字符串。

String output = zdt.format( f );


About java.time

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多