错误java.time.format.DateTimeParseException:无法分析,在索引10处找到未分析的文本
我正在尝试使用LocalDateTime pase下一个字符串,但我总是发现反分析文本错误:错误java.time.format.DateTimeParseException:无法分析,在索引10处找到未分析的文本,date,java-8,java-time,parse-error,localdate,Date,Java 8,Java Time,Parse Error,Localdate,我正在尝试使用LocalDateTime pase下一个字符串,但我总是发现反分析文本错误: 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” 我的代码是: public stati
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”
我的代码是:
public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException {
LocalDate dateTime =LocalDate.parse(convertDate);
return dateTime;
}
我想这并不太复杂,因为我看不出错误。字符串中的+02可能是原因吗?您的代码使用的是
LocalDate
,它只解析一个日期,而不是一个日期和时间,因此当解析找到日期后的空格时,您会收到一个错误
因此,您应该使用LocalDateTime
,但是LocalDateTime.parse(String)
需要一个ISO格式的日期,而不是您正在使用的格式
因此,您需要使用DateTimeFormatter
来指定输入字符串的格式。比如:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);
tl;博士
细节
对于该问题(使用仅日期对象作为日期时间值)正确,但解决方案不正确
这个答案使用了一些不必要的信息,这些信息不必要地丢弃了关于这个问题的有价值的信息。LocalDateTime
并不代表时间线上的特定时刻,只是根据调整到特定时区而对可能时刻的模糊概念
+02
是“提前两小时”的意思。因此,在UTC中,这一同时时刻的时间是12小时,比您的14小时少2小时。这确实代表了时间线上的一个特定时刻。这个偏移量是您用LocalDateTime
而不是OffsetDateTime
丢弃的有价值的信息
字符串的格式为SQL格式,接近标准ISO 8601格式。只需用“<代码> t>代码>替换中间的空间。默认情况下,java.time类使用ISO8601格式,因此无需指定格式模式
String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
不幸的是,Java8在解析仅缩写为一小时的偏移量值或忽略小时和分钟之间冒号的偏移量值时存在错误。已在Java9中修复。但是在Java8中,我们需要调整输入
// 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
System.out.println ( "input: " + input + " | inputModified: " + inputModified + " | odt: " + odt );
输入:2016-08-18 14:27:15.103+02 |输入修改:2016-08-18T14:27:15.103+02:00 | odt:2016-08-18T14:27:15.103+02:00
或者,指定格式模式。使用此格式模式时,偏移量解析错误不会出现
String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
数据库
从开始,您应该以日期时间对象而不是字符串的形式检索值
如果您的JDBC驱动程序符合JDBC 4.2的要求,则可以调用ResultSet::getObject
来获取Instant
或OffsetDateTime
。如果没有,则调用ResultSet::getTimestamp
获取java.sql.Timestamp
,然后通过调用Timestamp对象上的toInstant
立即转换为java.time
对于您的业务逻辑,请坚持使用java.time;简单地使用java.sql类型,并且仅用于与数据库的交换。我认为是日期和时间之间的空间。这是一个空格字符还是一个不间断的空格?如果您的字符串来自某种形式,则可能是与空格不同的字符。它来自Postgre数据库是!!!成功了!!!我以前也尝试过使用LocalDateTime,但是使用了WorngDateTimeFormatter。Thanx:)