[英]Java Date Format for Locale
如何找到给定Locale
的DateFormat
?
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 (eg 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();
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( Locale.CANADA_FRENCH );
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. 在生成表示日期时间值的String时,
DateTimeFormatter
有一种通过Locale
自动本地化的简单方法。 Specify a FormatStyle
to indicate length of output (abbreviated or not). 指定
FormatStyle
以指示输出的长度(缩写与否)。
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. 请注意,
Locale
和时区彼此无关。 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
,或者在这种情况下是印度的时区,其格式为Québécois人阅读。
ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = ZonedDateTime.now( z );
Generate a String using that localizing formatter object. 使用该本地化格式化程序对象生成String。
String output = zdt.format( f );
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧遗留日期时间类,如
java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 。 And search Stack Overflow for many examples and explanations.
并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 .
规范是JSR 310 。
Where to obtain the java.time classes? 从哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time.
该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as
Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.