简体   繁体   English

如何将Calendar.getInstance与指定的Locale一起使用

[英]How to use Calendar.getInstance with specified Locale

I am trying to use Calendar.getInstance(Locale l) with specified Locale and is not working. 我试图使用具有指定Locale Calendar.getInstance(Locale l)并且无法正常工作。 I cannot figure out what I am doing wrong. 我无法弄清楚我做错了什么。

The Java Doc. Java Doc。 say: 说:

getInstance public static Calendar getInstance(Locale aLocale) Gets a calendar using the default time zone and specified locale. getInstance public static Calendar getInstance(Locale aLocale)使用默认时区和指定的语言环境获取日历。 The Calendar returned is based on the current time in the default time zone with the given locale. 返回的日历基于具有给定语言环境的默认时区中的当前时间。 Parameters: aLocale - the locale for the week data Returns: a Calendar. 参数:aLocale - 周数据的区域设置返回:日历。

My code: 我的代码:

 public static void main (String[] args){

     Locale local = new Locale("pt", "BR");

     Calendar c = Calendar.getInstance(local); // here I am using the method
     System.out.println(c.getTime()); // and here, I cannot figure out why is not working


     DateFormat dt = DateFormat.getDateInstance(DateFormat.LONG, local);
     String s = dt.format(c.getTime());
     System.out.println(s); // here just a example in portuguese Brasil
 }

Output: 输出:

Wed Apr 29 10:18:16 BRT 2015 4月29日星期三10:18:16 BRT 2015

29 de Abril de 2015 29 de Abril de 2015

Should the first print must be in Locale("pt", "BR") , in portuguese? 第一个print必须是Locale("pt", "BR") ,用葡萄牙语?

The Answer by Loc is correct: Your call to Calendar::getTime produces a java.util.Date object. Loc答案是正确的:您对Calendar::getTime调用会生成一个java.util.Date对象。 The java.util.Date class has no explicit time zone yet its toString method confusingly applies the JVM's current default time zone while generating a String. java.util.Date类没有显式时区,但是在生成String时,其toString方法会混淆地应用JVM的当前默认时区。

All very confusing names and behavior - some of the many reasons to avoid these poorly designed, confusing, and troublesome old legacy date-time classes. 所有非常混乱的名称和行为 - 避免这些设计糟糕,令人困惑和麻烦的旧遗留日期时间类的许多原因中的一些。 Instead you should be using the java.time classes that officially supplant the old classes. 相反,你应该使用正式取代旧类的java.time类。

java.time java.time

Get the current moment in UTC . UTC格式获取当前时刻。 The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒 (最多九(9)位小数)。

Instant instant = Instant.now();

You can create a String to represent that value with standard ISO 8601 formatting by calling toString . 您可以通过调用toString创建一个String,以使用标准ISO 8601格式表示该值。

String output = instant.toString();

2016-09-28T19:38:21Z 2016-09-28T19:38:21Z

The code in the Question ignores the issue of time zone. 问题中的代码忽略了时区问题。 When you do not specify a time zone your JVM's current default time zone is implicitly applied. 如果未指定时区,则会隐式应用JVM的当前默认时区。 Better to specify explicitly. 最好明确指定。

Note that Locale and time zone are two completely separate distinct issues . 请注意, Locale和时区是两个完全独立的不同问题

  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such. 用于确定(a)用于翻译日期名称,月份名称等的人类语言的Locale ,以及(b)决定缩写,大小写,标点符号等问题的文化规范。
  • Time zone determines the wall-clock time used to present the date-time value. 时区确定用于显示日期时间值的挂钟时间

You can have any combination of the two. 你可以将两者结合起来。 For example, a time zone of Kolkata India with a French locale, or a Brazil Portuguese locale with an Auckland New Zealand time zone. 例如,加尔各答印度的时区,法国区域,或巴西葡萄牙语区域​​,奥克兰新西兰时区。

Locale locale = new Locale("pt", "BR");
ZoneId z = ZoneId.of( "Pacific/Auckland" );

Apply the time zone as a ZoneId to produce a ZonedDateTime . 将时区应用为ZoneId以生成ZonedDateTime Conceptually, think of it as ZonedDateTime = ( Instant + ZoneID ) . 从概念上讲,将其视为ZonedDateTime = ( Instant + ZoneID )

Specify a proper time zone name in the format of continent/region . continent/region格式指定适当的时区名称 Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!). 切勿使用3-4字母缩写,例如ESTIST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZonedDateTime zdt = instant.atZone( z );

The Locale does not affect the meaning, on the presentation. Locale不会影响演示文稿的含义。 We can let the Locale object drive the automatic localization of when producing a String to represent the date-time value via the DateTimeFormatter class. 我们可以让Locale对象在生成String时驱动自动本地化,以通过DateTimeFormatter类表示日期时间值。 Specify a FormatStyle to determine how long or abbreviated should the string be. 指定FormatStyle以确定字符串应该多长或缩写。

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
                                       .withLocale( locale );
String output = zdt.format( f );

Dump to console. 转储到控制台。 The instant and zdt objects seen here represent the very same moment, the same point on the timeline. 这里看到的instantzdt对象代表了同一时刻,即时间轴上的同一点。 The only difference is a view through the lens of a different region's wall-clock time. 唯一的区别是通过镜头观察不同地区的挂钟时间。

System.out.println ( "instant.toString(): " + instant 
                     + " | zdt: " + zdt 
                     + " | output: " + output );

instant.toString(): 2016-09-28T20:20:38.242Z | instant.toString():2016-09-28T20:20:38.242Z | zdt: 2016-09-29T09:20:38.242+13:00[Pacific/Auckland] | zdt:2016-09-29T09:20:38.242 + 13:00 [太平洋/奥克兰] | output: Quinta-feira, 29 de Setembro de 2016 09h20min38s NZDT 产量:Quinta-feira,29 de Setembro de 2016 09h20min38s NZDT

Conversion 转变

Avoid the old .Date and .Calendar classes. 避免使用旧的.Date.Calendar类。 But if you must use them with old code not yet updated for the java.time types, you can convert. 但是,如果必须将它们与尚未针对java.time类型更新的旧代码一起使用,则可以进行转换。 Use new methods added to the old classes. 使用添加到旧类的新方法。 Here we call java.util.GregorianCalendar.from( ZonedDateTime ) . 这里我们调用java.util.GregorianCalendar.from( ZonedDateTime )

java.util.Calendar cal = java.util.GregorianCalendar.from( zdt ) ;

And, going the other direction: 而且,走向另一个方向:

ZonedDateTime zdt = myGregorianCalendar.toZonedDateTime() ;

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old date-time classes such as java.util.Date , .Calendar , & java.text.SimpleDateFormat . 这些类取代了麻烦的旧日期时间类,如java.util.Date.Calendarjava.text.SimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to java.time. 现在处于维护模式Joda-Time项目建议迁移到java.time。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use… ). 大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植 ,并进一步适应的AndroidThreeTenABP (见如何使用...... )。

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 . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

Your first print is just print Date information & Java using Default locale for this print (English) 您的第一个打印是使用此打印的默认区域设置打印日期信息和Java(英语)

System.out.println(c.getTime());

This print will perform: c.getTime().toString() method, and this method using Default locale (ENGLISH) - You can take a look at Date.toString() source code to see Java using Default locale. 此打印将执行:c.getTime()。toString()方法,此方法使用默认语言环境(英语) - 您可以查看Date.toString()源代码以使用默认语言环境查看Java。

That is why the output is 'Wed Apr 29 10:18:16 BRT 2015' 这就是为什么输出是'Wed Apr 29 10:18:16 BRT 2015'

If you want print output is in portuguese. 如果你想要打印输出是葡萄牙语。 You must go with Second print. 你必须使用二次打印。

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

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