java.time.format.DateTimeParseException:无法在索引0处解析文本'Mi Mai 09 09:17:24 2018' [英] java.time.format.DateTimeParseException: Text 'Mi Mai 09 09:17:24 2018' could not be parsed at index 0

查看:259
本文介绍了java.time.format.DateTimeParseException:无法在索引0处解析文本'Mi Mai 09 09:17:24 2018'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用德语语言环境格式化日期时间对象,但出现错误。

  String dateString = Mi Mai 09 09:17:24 2018; 
DateTimeFormatter dtf = DateTimeFormatter.ofPattern( EE MMM dd HH:mm:ss yyyy,Locale.GERMAN);
LocalDateTime dateTime = LocalDateTime.parse(dateString,dtf);

错误:

  java.time.format.DateTimeParseException:无法在索引0 

解决方案

这里和现在的解决方案:

  System.setProperty( java.locale.providers, COMPAT,CLDR); 
String dateString = Mi Mai 09 09:17:24 2018;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern( EE MMM dd HH:mm:ss yyyy,Locale.GERMAN);
LocalDateTime dateTime = LocalDateTime.parse(dateString,dtf);
System.out.println(dateTime);

输出(在Java 9和Java 10上测试):


2018-05-09T09:17:24


代码中唯一的变化我在程序的开头插入了 System.setProperty( java.locale.providers, COMPAT,CLDR); 吗?根据文档,这实际上不应该工作,但是当我尝试过时它确实可以工作。相反,正确的方法是在运行Java程序时在命令行上提供相同的系统属性。例如:

  java -Djava.locale.providers = COMPAT,CLDR com.ajax.ParseTwoLetterDayOfWeekAbbreviationInGerman 

感谢乔·韦耶斯用于在注释中指出必须在命令行上设置该属性。有趣的是,同一系统属性破坏了Java 8上的代码。



更现代的解决方案:

  String dateString = Mi. Mai 09 09:17:24 2018; 

我在 Mi 表示它是缩写。



语言环境提供程序



解析日期Java中的Java需要所谓的语言环境数据,包括星期几以及德语和其他语言中使用的月份的名称和缩写。令人困惑的是,这些数据来自多个来源。在所有Java 8、9和10中,Java包含至少两个来源的语言环境数据,Java自己的语言环境数据和来自CLDR,Unicode通用语言环境数据存储库的标准化语言环境数据(我不知道是否还有一个或两个以上的数据源) 。在Java 8中,Java自己的数据是默认数据,如果需要这些数据,则需要指定CLDR。在Java 9及更高版本中,情况则相反:默认是CLDR,但是旧的Java数据可作为COMPAT获得。这将很快解释评论表示您的代码可在Java 8上运行,但在Java 10上则无法运行。



很明显,Java语言环境数据具有 Mi 不带点作为Mittwoch(星期三)的缩写,而CLDR的 Mi。带点。还有许多其他差异。



链接




I'm trying to format a date time object with a german locale but i got an error.

String dateString = "Mi Mai 09 09:17:24 2018";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss yyyy", Locale.GERMAN);
LocalDateTime dateTime = LocalDateTime.parse(dateString, dtf);

The error:

java.time.format.DateTimeParseException: Text 'Mi Mai 09 09:17:24 2018' could not be parsed at index 0

解决方案

Solution here and now:

System.setProperty("java.locale.providers", "COMPAT,CLDR");
String dateString = "Mi Mai 09 09:17:24 2018";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss yyyy", Locale.GERMAN);
LocalDateTime dateTime = LocalDateTime.parse(dateString, dtf);
System.out.println(dateTime);

Output (tested on Java 9 and Java 10):

2018-05-09T09:17:24

The only change from your code is I have inserted System.setProperty("java.locale.providers", "COMPAT,CLDR"); at the beginning of the program. According to the documentation this shouldn’t really work, but it did when I tried it. Instead the correct way is to supply the same system property on the command line when running your Java program. For example:

java -Djava.locale.providers=COMPAT,CLDR com.ajax.ParseTwoLetterDayOfWeekAbbreviationInGerman

Thanks to Joep Weijers for pointing out in a comment that the property must be set on the command line. Funnily the same system property broke the code on my Java 8.

More modern solution:

String dateString = "Mi. Mai 09 09:17:24 2018";

I have required a dot (a period) after Mi in the string to signify that it is an abbreviation.

Locale providers

To parse a date in German Java needs so-called locale data, including the names and abbreviations for days of the week and for months used in German and other languages. To confuse things, these data come from more than one source. In all of Java 8, 9 and 10 Java contains locale data from at least two sources, Java’s own locale data and standardized locale data from CLDR, Unicode Common Locale Data Repository (I don’t know if there is one or two more sources). In Java 8 Java’s own data were the default and you would need to specify CLDR if you wanted those data instead. In Java 9 and later it’s the other way around: CLDR is the default, but the old Java data are available as COMPAT. This explains the comment by soon that your code works on Java 8, but fails on Java 10.

And apparently the Java locale data have Mi without a dot as abbreviation for Mittwoch (Wednesday), while CLDR has Mi. with a dot. There are many other differences.

Links

这篇关于java.time.format.DateTimeParseException:无法在索引0处解析文本'Mi Mai 09 09:17:24 2018'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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