错误java.time.format.DateTimeParseException:无法解析,在索引10处找到未解析的文本 [英] Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10

查看:721
本文介绍了错误java.time.format.DateTimeParseException:无法解析,在索引10处找到未解析的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LocalDateTime来设置下一个String,但我总是得到de unparsed text found错误:

I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error:

Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10

这是我的字符串:convertDate:' 2016-08-18 14:27:15.103 + 02 '

Here is my String: convertDate: '2016-08-18 14:27:15.103+02'

我的代码:

public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException {
    LocalDate dateTime =LocalDate.parse(convertDate);
    return dateTime;
}

我想不是太复杂,买我不能看到错误。字符串中的 +02 可能是原因吗?

I guess is not too complicated, buy I´m not able to see the error. Could the +02 in the String be the cause?

推荐答案

tl; dr



tl;dr

OffsetDateTime odt = OffsetDateTime.parse ( "2016-08-18 14:27:15.103+02" , DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" ) ) ;



详细信息



按greg-449审核对于问题是正确的(使用仅日期对象作为日期时间值)但不是解决方案。

Details

The Answer by greg-449 is correct about the problem (using a date-only object for a date-time value) but not the solution.

该答案使用 LocalDateTime 这会不必要地丢弃有关从UTC偏移 LocalDateTime 代表时间轴上的特定时刻,只是对可能时刻的模糊概念,具体取决于调整到特定时区。

That Answer uses LocalDateTime which unnecessarily throws away valuable information about the offset-from-UTC. A LocalDateTime does not represent a specific moment on the timeline, only a vague idea about possible moments depending on adjusting into a particular time zone.

+02 offset-from-UTC 表示提前两小时 UTC 。所以在UTC中,这个同步时刻的时间是12小时,比14小时少2小时。此 代表时间轴上的特定时刻。此偏移量是您使用 LocalDateTime 而不是 OffsetDateTime 丢弃的有价值信息。

The +02 is an offset-from-UTC meaning "two hours ahead of UTC". So in UTC the time-of-day for this simultaneous moment is 12 hours, 2 hours less than your 14 hours. This does represent a specific moment on the timeline. This offset is the valuable information you are throwing away with a LocalDateTime rather than an OffsetDateTime.

您的字符串格式为SQL格式,接近标准ISO 8601格式。只需用 T 替换中间的SPACE。默认情况下,java.time类使用ISO 8601格式,因此无需指定格式化模式。

The format of your string is in SQL format, which is close to standard ISO 8601 format. Merely replace the SPACE in the middle with a T. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.

String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );

不幸的是,Java 8在解析缩写为一小时的偏移值或忽略的偏移值方面存在错误小时和分钟之间的结肠。已在Java 9中修复。但在Java 8中,我们需要调整输入。

Unfortunately, Java 8 has a bug in parsing offset values abbreviated to just an hour or offset values omitting the colon between hours and minutes. Fixed in Java 9. But in Java 8, we need to adjust the input.

// Workaround for Java 8 where 2-digit offset fails parsing. Fixed in Java 9.
int lengthOfAbbreviatedOffset = 3;
if ( inputModified.indexOf ( "+" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
    // If third character from end is a PLUS SIGN, append ':00'.
    inputModified = inputModified + ":00";
}
if ( inputModified.indexOf ( "-" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
    // If third character from end is a PLUS SIGN, append ':00'.
    inputModified = inputModified + ":00";
}

现在解析。

OffsetDateTime odt = OffsetDateTime.parse ( inputModified );

转储到控制台。请注意我们如何将 +02 转换为 +02:00

Dump to console. Note how we transformed +02 into +02:00.

System.out.println ( "input: " + input + " | inputModified: " + inputModified + " | odt: " + odt );




输入:2016-08-18 14:27:15.103 + 02 | inputModified:2016-08-18T14:27:15.103 + 02:00 | odt:2016-08-18T14:27:15.103 + 02:00

input: 2016-08-18 14:27:15.103+02 | inputModified: 2016-08-18T14:27:15.103+02:00 | odt: 2016-08-18T14:27:15.103+02:00

或者,指定格式化模式。使用此格式化模式时,偏移解析错误不会发生。

Alternatively, specify a formatting pattern. The offset-parsing bug does not bite when using this formatting pattern.

    DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
    OffsetDateTime odt = OffsetDateTime.parse ( input , f );



数据库



来自 Postgres ,您应该将值检索为日期时间对象而不是字符串。

Database

Coming from Postgres, you should be retrieving the value as a date-time object rather than a String.

如果您的JDBC驱动程序符合JDBC 4.2,您可以调用 ResultSet :: getObject 来获得即时 OffsetDateTime 。如果没有,请调用 ResultSet :: getTimestamp 获取 java.sql.Timestamp ,然后立即转换为java.time通过在Timestamp对象上调用 toInstant

If your JDBC driver complies with JDBC 4.2 you can call ResultSet::getObject to get an Instant or OffsetDateTime. If not, call ResultSet::getTimestamp to get a java.sql.Timestamp, then immediately convert to java.time by calling toInstant on the Timestamp object.

坚持使用java.time作为业务逻辑;简单地使用java.sql类型,仅用于与数据库交换。

Stick with java.time for your business logic; use the java.sql types briefly and only for exchange with the database.

这篇关于错误java.time.format.DateTimeParseException:无法解析,在索引10处找到未解析的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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