什么会导致 joda 时间的输出差异?

What could cause this difference in output in joda time?

这个测试程序:

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Locale;

public class Test
{
    public static void main( String[] args )
    {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern( "dd-MMM-yyyy H:mm:ss z" ).withLocale( Locale.FRANCE );
        String print = dateTimeFormatter.print( new DateTime() );
        System.out.println( "print = " + print );
    }
}

在 Java 1.8.0_11 和 1.8.0_60 上给出不同的输出:

更新 11:11-sept.-2015 16:23:38 CEST

更新 60:11-sept.-2015 16:21:46 +02:00

两者都在使用 Joda Time 2.6。知道这是为什么吗?

(将 Java 6 与 Joda Time 2.0 一起使用也会得到 11-sept.-2015 16:31:07 CEST

参见 release notes of JodaTime 2.8.1 与 JDK 8u60 相关的内容。

Changes in 2.8.1

  • Fixed to handle JDK 8u60 [#288, #291] > Without this fix, formatting a time-zone will print "+00:00" instead of "GMT" for the GMT time-zone

和相应的错误报告。

Joda Time 使用 DefaultNameProvider#getNameSet() 查找时区名称。反过来使用底层 JDK 的 java.text.DateFormatSymbols#getZoneStrings() 其中 returns String[][].

在 JodaTime <= 2.8 DefaultNameProvider#getNameSet() 中,代码如下所示:

if (strings != null && strings.length == 5 && id.equals(strings[0])) {
    // ...
    // we have found the time zone name
}

stringsDateFormatSymbols#getZoneStrings() 返回的 String[][] 数组的一个元素。注意 strings.length == 5.

使用 JDK < 8u60 DateFormatSymbols#getZoneStrings() 返回 5 元素数组,例如

[America/Los_Angeles, Pacific Standard Time, PST, Pacific Daylight Time, PDT]

因为 JDK 8u60 它 returns 7 个元素的数组,例如

[America/Los_Angeles, Pacific Standard Time, PST, Pacific Daylight Time, PDT, Pacific Time, PT]

所以 strings.length == 5 条件失败。这已在 Joda Time 2.8.1 中更改为 strings.length >= 5 并再次工作(打印 "CEST")。