gpt4 book ai didi

java - 如何使用具有指定语言环境的 Calendar.getInstance

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:44:34 25 4
gpt4 key购买 nike

我正在尝试使用 Calendar.getInstance(Locale l)指定 Locale并且不工作。我无法弄清楚我做错了什么。

Java 文档。说:

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

29 de Abril de 2015



应该先 print必须在 Locale("pt", "BR") , 在葡萄牙语?

最佳答案

Answer by Loc正确:您调用 Calendar::getTime 产生 java.util.Date 目的。 java.util.Date类没有明确的时区,但它的 toString方法在生成字符串时会混淆地应用 JVM 的当前默认时区。

所有非常令人困惑的名称和行为 - 避免这些设计不良、令人困惑和麻烦的旧旧日期时间类的众多原因中的一些。相反,您应该使用正式取代旧类的 java.time 类。

java.time

UTC 中获取当前时刻. Instant 类表示UTC 中时间轴上的时刻分辨率为 nanoseconds (最多九 (9) 位小数)。

Instant instant = Instant.now();

您可以使用标准 ISO 8601 创建一个字符串来表示该值。通过调用 toString 进行格式化.
String output = instant.toString();

2016-09-28T19:38:21Z



问题中的代码忽略了时区问题。当您不指定时区时,您的 JVM 当前默认时区将被隐式应用。最好明确指定。

请注意 Locale和时区是两个完全不同的不同问题 .
  • Locale 确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号等问题的文化规范。
  • 时区决定wall-clock time用于显示日期时间值。

  • 您可以将两者任意组合。例如,加尔各答印度的时区具有法语语言环境,或巴西葡萄牙语言环境具有奥克兰新西兰时区。
    Locale locale = new Locale("pt", "BR");
    ZoneId z = ZoneId.of( "Pacific/Auckland" );

    将时区应用为 ZoneId制作 ZonedDateTime .从概念上讲,将其视为 ZonedDateTime = ( Instant + ZoneID ) .

    指定 proper time zone name格式为 continent/region .切勿使用 3-4 个字母的缩写,例如 ESTIST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
    ZonedDateTime zdt = instant.atZone( z );
    Locale不影响意思,就介绍。我们可以让 Locale对象通过 DateTimeFormatter 驱动生成字符串时的自动本地化以表示日期时间值类(class)。指定 FormatStyle 确定字符串的长度或缩写。
    DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
    .withLocale( locale );
    String output = zdt.format( f );

    转储到控制台。 instantzdt此处看到的物体代表同一时刻,时间轴上的同一点。唯一的区别是从不同地区的挂钟时间的角度来看。
    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[Pacific/Auckland] | output: Quinta-feira, 29 de Setembro de 2016 09h20min38s NZDT



    转换

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

    而且,走向另一个方向:
    ZonedDateTime zdt = myGregorianCalendar.toZonedDateTime() ;

    关于 java.time

    java.time框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧日期时间类,例如 java.util.Date , .Calendar , & java.text.SimpleDateFormat .

    Joda-Time项目,现在在 maintenance mode ,建议迁移到 java.time。

    要了解更多信息,请参阅 Oracle Tutorial .并在 Stack Overflow 上搜索许多示例和解释。

    许多 java.time 功能在 ThreeTen-Backport 中向后移植到 Java 6 和 7。并进一步适应 AndroidThreeTenABP (见 How to use…)。

    ThreeTen-Extra项目通过附加类扩展了 java.time。该项目是 future 可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如 Interval , YearWeek , YearQuarter , 和 more .

    关于java - 如何使用具有指定语言环境的 Calendar.getInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945084/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com