WeekField在JVM 8和JVM 10上的不同行为 [英] Different behavior of WeekFields on JVM 8 and JVM 10

查看:241
本文介绍了WeekField在JVM 8和JVM 10上的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个非常简单的程序:

I have really simple program here:

 public static void main(String[] args) {
        LocalDate year = LocalDate.ofYearDay(2022, 100);
        System.out.println(year);

        System.out.println(WeekFields.of(Locale.GERMAN).weekOfYear());

        System.out.println(year.with(WeekFields.of(Locale.GERMAN).weekOfYear(), 0));
        System.out.println(year.with(WeekFields.of(Locale.GERMAN).weekOfYear(), 0).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
    }

但是它在JVM 8和JVM 10上的行为有所不同.问题似乎出在WeekFields.of(Locale.GERMAN).weekOfYear()的实现上.

But it behaves differently on JVM 8 and JVM 10. The problem seems to be implementation of WeekFields.of(Locale.GERMAN).weekOfYear().

在JVM 10上,我得到以下结果:

On JVM 10 I get following results:

JVM 10

2022-04-10
WeekOfYear[WeekFields[SUNDAY,1]]
2021-12-19
2021-12-13

而在JVM 8上:

JVM 8

2022-04-10
WeekOfYear[WeekFields[MONDAY,4]]
2022-01-02
2021-12-27

为什么会这样?我是否正在做某事,可能会导致不确定的行为?还是在某处指定了这种行为变化?

Why is this happening? Am I doing something, that could potentially cause undefined behavior? Or is this change in behavior somewhere specified?

JVM10:

$ java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)

JVM8

$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

JVM 9具有与JVM 8相同的行为,并且JVM 11的行为类似于JVM 10

JVM 9 has same behavior as JVM 8 and JVM 11 behaves like JVM 10

我实际上找到了更改行为的提交-> 在github上我很好奇为什么要更改它.

EDIT 2: I actually found the commit which changed the behavior -> here on github and I'm curious why this was changed.

推荐答案

这样的星期字段是高度本地化的,因此取决于基础JVM的本地化资源,该资源可以从一个发行版更改为另一个发行版.

Such week fields are highly localized, and hence dependent on the localized resources of the underlying JVM which can change from one release to another.

我认为JVM10更正确,因为Locale.GERMAN并未指向任何国家,因此Java简单地假定了美国(某种程度上将这个国家作为世界标准来处理这个问题,但Java也是这样).

I think JVM10 is more correct because Locale.GERMAN does not refer to any country so Java simple assumes US (somehow questionable to handle this country as world standard, but so is Java).

您最好使用 Locale.GERMANY.该国家确实确实将星期一作为一周的第一天(与美国从星期日开始的情况相反,美国将周日用作GERMAN的后备广告,这只是一种语言,而不是一个国家/地区.

You should better use Locale.GERMANY. That country indeed does use Monday as first day of week (in contrast to US starting on Sunday which is used as fallback for GERMAN which is just a language and not a country.

后备国家/地区的当前的CLDR数据列表/区域"001"(=世界范围)的周定义(星期一为一周的第一天,1 =日历年第一周的最少天数).令人惊讶的是,这与美国定义不同(星期日,1).我认为,Oracle只是做了自己的事.就个人而言,我同意@Holger的看法,而是希望将ISO-8601作为后备广告(星期一,4).

The current CLDR data list for the fallback country/territory "001" (= worldwide) the week definitions (Monday as first day of week and 1 = minimal days of first week in calendar year). Astonishingly, this is different from US-definition (Sunday, 1). I think, Oracle has just done its own thing. Personally, I agree with @Holger and rather expect ISO-8601 as fallback (Monday, 4).

但是,通过设置以下

However, you might be able to restore the Java-8-behaviour on your JVM-10-machine by setting following system property (not tested):

java.locale.providers=COMPAT,CLDR,SPI

这篇关于WeekField在JVM 8和JVM 10上的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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