5 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個贊
我一直在查看 的源代碼DateTimeFormatterBuilder,但我不確定,但在我看來,您的一個可能原因NullPointerException是某些區(qū)域設(shè)置數(shù)據(jù)中的空時區(qū)縮寫。這可能會導(dǎo)致DateTimeFormatterBuilder.ZoneTextPrinterParser.getTree()將 null 傳遞給DateTimeFormatterBuilder.PrefixTree.add(),而后者又不期望 null。如果是這樣,則不同的行為可能是由不同時區(qū)和不同區(qū)域設(shè)置的組合引起的。請注意,時區(qū)和語言環(huán)境是獨(dú)立的。
編輯:提問者報(bào)告說我的建議沒有解決這個特定問題。我保留它是因?yàn)槲艺J(rèn)為指定首選時區(qū)以解析時區(qū)縮寫的可能性可能對其他幾個人有所幫助。
我沒有重現(xiàn)您的異常,因此無法給出確定的修復(fù)方法,但我建議您嘗試:
Set<ZoneId> preferredZones = Set.of(ZoneId.of("America/Goose_Bay"),
ZoneId.of("America/Moncton"), ZoneId.of("America/New_York"),
ZoneId.of("America/Chicago"), ZoneId.of("America/Denver"),
ZoneId.of("America/Los_Angeles"), ZoneId.of("America/Anchorage"),
ZoneId.of("Pacific/Honolulu"), ZoneId.of("America/Adak"),
ZoneId.of("Pacific/Pago_Pago"), ZoneId.of("Pacific/Guam"));
DateTimeFormatter hhmm_a_zzz_EEE_MMM_dd_yyyy = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("hmm a ")
.appendZoneText(TextStyle.SHORT, preferredZones)
.appendPattern(" EEE MMM d yyyy")
.toFormatter(Locale.US);
已選擇首選區(qū)域以匹配您鏈接到的 17 個縮寫:
AST America/Goose_Bay, America/Moncton
EST EDT America/New_York
CST CDT America/Chicago
MST MDT America/Denver
PST PDT America/Los_Angeles
AKST AKDT America/Anchorage
HST Pacific/Honolulu
HAST HADT America/Adak
SST SDT Pacific/Pago_Pago
CHST Pacific/Guam
您可能想檢查我的映射是否正確。
此外,正如其他人已經(jīng)建議的那樣,我已經(jīng)Locale.US為格式化程序指定了。

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個贊
的確,這很神秘。null
作為嘗試生成錯誤的一部分,這似乎很奇怪。我無法重現(xiàn)您的問題,我已經(jīng)嘗試了 2 個 Java 8 和 12 環(huán)境。
我有幾個建議:
Locale
在解析諸如日期名稱或月份名稱之類的文本時始終指定 a 。在單獨(dú)的方法中,將代碼簡化到絕對最小值,以確保沒有副作用。
通過調(diào)用轉(zhuǎn)儲有關(guān) JVM 環(huán)境的信息
System.getProperties()
。
代碼:
package work.basil.example;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class App {
public static void main ( String[] args ) {
System.out.println ( "Hello World!" );
App app = new App ();
app.doIt ();
}
private void doIt ( ) {
DateTimeFormatterBuilder builder =
new DateTimeFormatterBuilder ()
.parseCaseInsensitive ()
.appendPattern ( "hmm a zzz EEE MMM d yyyy" );
Locale locale = Locale.US;
DateTimeFormatter f = builder.toFormatter ( locale );
String input = "948 AM MDT Wed Jul 10 2019";
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );
System.out.println ( "zdt.toString() = " + zdt );
System.out.println ( System.getProperties () );
}
}
我成功地運(yùn)行了這個:
我自己的 Mac Mini (2018) 和 macOS Mojave 10.14.5 使用來自 AdoptOpenJDK 和 HotSpot 的 Java 12.0.1+12。
IdeOne.com 從帶有 HotSpot 的 Oracle JDK 運(yùn)行 java.version=1.8.0_112。查看此代碼實(shí)時運(yùn)行。
帶有來自 AdoptOpenJDK.net 的 OpenJDK 11.0.3+7 的 Ubuntu 18.04.1 LTS(在 Mac 上的 Parallels 虛擬機(jī)中運(yùn)行)。
帶有來自Oracle 站點(diǎn)的 Oracle JDK 11.0.3+12 的 Ubuntu 18.04.2 LTS 。
所有這些結(jié)果相同。
zdt.toString() = 2019-07-10T09:48-06:00[美國/丹佛]
系統(tǒng)屬性
蘋果
{awt.toolkit=sun.lwawt.macosx.LWCToolkit,java.specification.version=12,sun.jnu.encoding=UTF-8,java.class.path=/Users/basilbourque/IdeaProjects/Demo/target/classes: /Users/basilbourque/.m2/repository/org/threeten/threeten-extra/1.5.0/threeten-extra-1.5.0.jar, java.vm.vendor=AdoptOpenJDK, sun.arch.data.model=64, java.vendor.url= https://adoptopenjdk.net/ , java.vm.specification.version=12, os.name=Mac OS X, sun.java.launcher=SUN_STANDARD, user.country=US, sun.boot .library.path=/Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home/lib, sun.java.command=work.basil.example.App, http.nonProxyHosts=local| .local|169.254/16|.169.254/16, jdk.debug=release, sun.cpu.endian=little, user.home=/Users/basilbourque, user.language=en, java.specification.vendor=Oracle Corporation, java.version.date=2019 -04-16,java.home=/Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home,file.separator=/,java.vm.compressedOopsMode=零基礎(chǔ),line.separator=,java.vm。 specification.vendor=Oracle Corporation,java.specification.name=Java 平臺 API 規(guī)范,java.awt.graphicsenv=sun.awt.CGraphicsEnvironment,sun.management.compiler=HotSpot 64 位分層編譯器,ftp.nonProxyHosts=local| .local|169.254/16|.169.254/16, java.runtime.version=12.0.1+12, user.name=basilbourque, path.separator=:, os.version=10.14.5, java.runtime.name=OpenJDK Runtime Environment, file.encoding =UTF-8,java.vm.name=OpenJDK 64 位服務(wù)器 VM,java.vendor.version=AdoptOpenJDK,java.vendor.url.bug= https://github.com/AdoptOpenJDK/openjdk-build/issues,java.io.tmpdir=/var/folders/qk/grjjffnj7ml_r54rrb1c2pbw0000gn/T/,java.version=12.0.1,user.dir=/Users/basilbourque/IdeaProjects/Demo,os.arch=x86_64,java.vm。 specification.name=Java虛擬機(jī)規(guī)范,java.library.path=/Users/basilbourque/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions :/usr/lib/java:., java.vm.info=混合模式, 共享, java.vendor=AdoptOpenJDK, java.vm.version=12.0.1+12, sun.io.unicode.encoding=UnicodeBig, socksNonProxyHosts =本地| .local|169.254/16| .169.254/16,java.class.version=56.0}
IdeOne.com
{java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=/opt/jdk/jre/lib/amd64, java.vm.version=25.112-b15, java.vm.vendor=甲骨文公司,java.vendor.url= http://java.oracle.com/, path.separator=:, java.vm.name=Java HotSpot(TM) 64 位服務(wù)器 VM, file.encoding.pkg=sun.io, user.country=US, sun.java.launcher=SUN_STANDARD, sun. os.patch.level=未知,java.vm.specification.name=Java 虛擬機(jī)規(guī)范,user.dir=/home/uXdFYs,java.runtime.version=1.8.0_112-b15,java.awt.graphicsenv=sun。 awt.X11GraphicsEnvironment,java.endorsed.dirs=/opt/jdk/jre/lib/endorsed,os.arch=amd64,java.io.tmpdir=/tmp,line.separator=,java.vm.specification.vendor=Oracle公司,os.name=Linux,sun.jnu.encoding=ANSI_X3.4-1968,java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr /lib,java.specification.name=Java 平臺 API 規(guī)范,java.class.version=52.0,sun.management.compiler=HotSpot 64 位分層編譯器,os.version=3.16.0-4-amd64,user.home =?, user.timezone=, java.awt.printerjob=sun.print。PSPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, java.class.path=tested.zip, user.name=?, java.vm.specification.version=1.8, sun.java.command= tested.zip,java.home=/opt/jdk/jre,sun.arch.data.model=64,user.language=en,java.specification.vendor=Oracle Corporation,awt.toolkit=sun.awt.X11。 XToolkit,java.vm.info=混合模式,java.version=1.8.0_112,java.ext.dirs=/opt/jdk/lib,sun.boot.class.path=/opt/jdk/jre/lib/resources .jar:/opt/jdk/jre/lib/rt.jar:/opt/jdk/jre/lib/sunrsasign.jar:/opt/jdk/jre/lib/jsse.jar:/opt/jdk/jre/lib /jce.jar:/opt/jdk/jre/lib/charsets.jar:/opt/jdk/jre/lib/jfr.jar:/opt/jdk/jre/classes, java.vendor=Oracle Corporation, file.separator =/, java.vendor.url.bug=zip,java.home=/opt/jdk/jre,sun.arch.data.model=64,user.language=en,java.specification.vendor=Oracle Corporation,awt.toolkit=sun.awt.X11.XToolkit, java.vm.info=混合模式,java.version=1.8.0_112,java.ext.dirs=/opt/jdk/lib,sun.boot.class.path=/opt/jdk/jre/lib/resources.jar :/opt/jdk/jre/lib/rt.jar:/opt/jdk/jre/lib/sunrsasign.jar:/opt/jdk/jre/lib/jsse.jar:/opt/jdk/jre/lib/jce .jar:/opt/jdk/jre/lib/charsets.jar:/opt/jdk/jre/lib/jfr.jar:/opt/jdk/jre/classes, java.vendor=Oracle Corporation, file.separator=/ , java.vendor.url.bug=zip,java.home=/opt/jdk/jre,sun.arch.data.model=64,user.language=en,java.specification.vendor=Oracle Corporation,awt.toolkit=sun.awt.X11.XToolkit, java.vm.info=混合模式,java.version=1.8.0_112,java.ext.dirs=/opt/jdk/lib,sun.boot.class.path=/opt/jdk/jre/lib/resources.jar :/opt/jdk/jre/lib/rt.jar:/opt/jdk/jre/lib/sunrsasign.jar:/opt/jdk/jre/lib/jsse.jar:/opt/jdk/jre/lib/jce .jar:/opt/jdk/jre/lib/charsets.jar:/opt/jdk/jre/lib/jfr.jar:/opt/jdk/jre/classes, java.vendor=Oracle Corporation, file.separator=/ , java.vendor.url.bug=/opt/jdk/jre/lib/sunrsasign.jar:/opt/jdk/jre/lib/jsse.jar:/opt/jdk/jre/lib/jce.jar:/opt/jdk/jre/lib/charsets。 jar:/opt/jdk/jre/lib/jfr.jar:/opt/jdk/jre/classes, java.vendor=Oracle 公司, file.separator=/, java.vendor.url.bug=/opt/jdk/jre/lib/sunrsasign.jar:/opt/jdk/jre/lib/jsse.jar:/opt/jdk/jre/lib/jce.jar:/opt/jdk/jre/lib/charsets。 jar:/opt/jdk/jre/lib/jfr.jar:/opt/jdk/jre/classes, java.vendor=Oracle 公司, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/ , sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=}

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個贊
這終于是一個已知問題,在 Fedora 中解決了,但在 Redhat 中還沒有解決:
https://bugzilla.redhat.com/show_bug.cgi?id=1837376 https://bugzilla.redhat.com/show_bug.cgi?id=1838229.
問題是有缺陷的 /usr/share/javazi-1.8/tzdb.dat。

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個贊
我以前遇到過同樣的問題,對我有用的是.toFormatter()
改為.toFormatter(Locale.US)
. 我不確定是什么導(dǎo)致了這個問題,它一定與機(jī)器的時區(qū)有關(guān)??纯催@是否適合你。

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個贊
在 CentOS 上,只有發(fā)行版中打包的 jvm 11 和 14 失敗。他們被確定為:
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)
或者:
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment 20.3 (build 14.0.1+7)
OpenJDK 64-Bit Server VM 20.3 (build 14.0.1+7, mixed mode, sharing)
從 AdoptOpenJDK 下載,一切正常:
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
或者
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
我的 Mac 上也有幾乎所有的 JVM Since 1.8。他們都很好。
測試的格式是:
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d HH:mm:ss zzz", Locale.ENGLISH);
(編輯)代碼接近所示示例:
import java.time.*;
import java.time.format.*;
import java.util.Locale;
public class DateTest {
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM d HH:mm:ss zzz", Locale.ENGLISH);
public static void main(String[] args) {
System.out.println(dtf.parse("Jul 15 11:20:01 CEST"));
}
}
以及 JVM 14 的堆棧:
/usr/lib/jvm/jre-14/bin/java -XX:+ShowCodeDetailsInExceptionMessages -cp . DateTest 2>&1 | sort -u | less
... 1 more
at DateTest.main(DateTest.java:9)
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2021)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1878)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1882)
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2040)
at java.base/java.time.format.DateTimeFormatter.parseUnresolved0(DateTimeFormatter.java:2111)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.parse(DateTimeFormatterBuilder.java:2372)
at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add(DateTimeFormatterBuilder.java:4402)
at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add0(DateTimeFormatterBuilder.java:4407)
at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.prefixLength(DateTimeFormatterBuilder.java:4538)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneIdPrinterParser.parse(DateTimeFormatterBuilder.java:4260)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.getTree(DateTimeFormatterBuilder.java:4149)
Caused by: java.lang.NullPointerException: Cannot invoke "String.length()" because "k" is null
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Jul 15 11:20:01 CEST' could not be parsed: Cannot invoke "String.length()" because "k" is null
添加回答
舉報(bào)