语言环境的 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
Java Date Format for Locale
提问by
How can I find the DateFormat
for 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 Date
s:
然后你可以使用这个对象来格式化Date
s:
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 SimpleDateFormat
object 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 DateTimeFormatter
has a simple way to localize automatically by Locale
when generating a String to represent the date-time value. Specify a FormatStyle
to 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 Locale
and 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 Locale
of 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 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABPproject adapts ThreeTen-Backport(mentioned above) for Android specifically.
- See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 所述ThreeTenABP项目适应ThreeTen-反向移植(上述)为Android特异性。
- 请参阅如何使用ThreeTenABP ...。
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 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。