如何将Calendar.getInstance与指定的Locale一起使用 [英] How to use Calendar.getInstance with specified Locale

查看:202
本文介绍了如何将Calendar.getInstance与指定的Locale一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用指定区域设置 Calendar.getInstance(Locale l),但无效。我无法弄清楚我做错了什么。

I am trying to use Calendar.getInstance(Locale l) with specified Locale and is not working. I cannot figure out what I am doing wrong.

Java Doc。说:

The Java Doc. say:


getInstance
public static Calendar getInstance(Locale aLocale)

获取一个使用默认时区和指定区域设置的日历。 返回的日历基于具有给定区域设置的默认时区中的当前时间。
参数:
aLocale - 周数据的区域设置
返回:
a日历。

getInstance public static Calendar getInstance(Locale aLocale) Gets a calendar using the default time zone and specified locale. 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.

我的代码:

 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
 }

输出:


Wed Apr 29 10:18:16 BRT 2015

Wed Apr 29 10:18:16 BRT 2015

29 de Abril de 2015

29 de Abril de 2015

第一个 print 是否必须在区域设置(pt ,BR),用葡萄牙语?

Should the first print must be in Locale("pt", "BR"), in portuguese?

推荐答案

通过Loc的答案是正确的:您对 Calendar :: getTime 生成 java.util.Date 对象。 java.util.Date 类没有明确的时区,但其 toString 方法容易混淆地应用JVM的当前默认时区同时生成一个字符串。

The Answer by Loc is correct: Your call to Calendar::getTime produces a java.util.Date object. 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.time类。

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.

通过 UTC 获取当前时刻。 Instant 类代表 UTC 中时间轴上的一个时刻,其分辨率为纳秒(最多十(9)位小数)。

Get the current moment in 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 instant = Instant.now();

您可以使用标准 ISO 8601 格式通过调用 toString

You can create a String to represent that value with standard ISO 8601 formatting by calling toString.

String output = instant.toString();




2016-09-28T19:38:21Z

2016-09-28T19:38:21Z

问题中的代码忽略了时区问题。如果未指定时区,则会隐式应用JVM的当前默认时区。最好明确指定。

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. Better to specify explicitly.

请注意 区域设置和时区是两个完全独立的不同问题

Note that Locale and time zone are two completely separate distinct issues.


  • 区域设置 确定(a)用于翻译日期名称,月份名称等的人类语言, (b)文化规范决定缩写,大写,标点符号等问题。

  • 时区确定挂钟时间用于显示日期时间值。

  • 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.
  • 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" );

将时区应用为 ZoneId 至产生 ZonedDateTime 。从概念上讲,将其视为 ZonedDateTime =(Instant + ZoneID)

Apply the time zone as a ZoneId to produce a ZonedDateTime. Conceptually, think of it as ZonedDateTime = ( Instant + ZoneID ).

指定正确的时区名称格式为 continent / region 。切勿使用3-4字母缩写,例如 EST IST ,因为它们不是真正的时区,不是标准化的,甚至没有独特的(!)。

Specify a proper time zone name in the format of 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(!).

ZonedDateTime zdt = instant.atZone( z );

区域设置不影响含义,在演示文稿上。我们可以让 Locale 对象驱动生成String的自动本地化,以通过 DateTimeFormatter 上课。指定 FormatStyle 确定字符串的长度或缩写。

The Locale does not affect the meaning, on the presentation. 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. Specify a FormatStyle to determine how long or abbreviated should the string be.

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

转储到控制台。此处显示的即时 zdt 对象代表了同一时刻,即时间轴上的相同点。唯一的区别是通过镜头观察不同地区的挂钟时间。

Dump to console. The instant and zdt objects seen here represent the very same moment, the same point on the timeline. 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 | zdt:2016-09-29T09:20:38.242 + 13:00 [太平洋/奥克兰] |输出:Quinta-feira,29 de Setembro de 2016 09h20min38s NZDT

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



转换



避免旧的 .Date .Calendar 类。但是,如果必须将它们与尚未针对java.time类型更新的旧代码一起使用,则可以进行转换。使用添加到旧类的新方法。这里我们称之为 java.util.GregorianCalendar.from(ZonedDateTime)

Conversion

Avoid the old .Date and .Calendar classes. But if you must use them with old code not yet updated for the java.time types, you can convert. Use new methods added to the old classes. Here we call java.util.GregorianCalendar.from( ZonedDateTime ).

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

并向另一个方向发展:

ZonedDateTime zdt = myGregorianCalendar.toZonedDateTime() ;



关于java.time



< a href =http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html =nofollow noreferrer> java.time 框架内置于Java 8及更高版本。这些类取代了麻烦的旧日期时间类,例如 java.util.Date .Calendar ,& java.text.SimpleDateFormat

About java.time

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

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

The Joda-Time project, now in maintenance mode, advises migration to java.time.

要了解更多信息,请参阅 Oracle教程。并搜索Stack Overflow以获取许多示例和解释。

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

大部分java.time功能都被反向移植到Java 6& 7在 ThreeTen-Backport 中进一步适应 ThreeTenABP中的rel =nofollow noreferrer> Android (请参阅 如何使用...... )。

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…).

ThreeTen-Extra 项目使用其他类扩展了java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,例如 Interval YearWeek YearQuarter 更多

The ThreeTen-Extra project 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.

这篇关于如何将Calendar.getInstance与指定的Locale一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆