问题描述
我正在尝试将存储在数据库中的String值(最初是LocalDateTime变量)转换为datetime,并将其解析为LocalDateTime变量。我已经用格式化程序尝试过:
I'm trying to convert a String value (initially a LocalDateTime variable) that was stored in a database (as datetime) and parse it into a LocalDateTime variable. I've tried it with a formatter:
String dTP;
dTP=(rs.getString("arrivedate"));
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTimeParked = LocalDateTime.parse(dTP,formatter);
并且没有格式化程序:
String dTP;
dTP=(rs.getString("arrivedate"));
LocalDateTime dateTimeParked = LocalDateTime.parse(dTP);
但是每次我都会遇到相同的错误:
But I get the same error each time:
Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '2016-07-09 01:30:00.0' could not be parsed at index 10
我的想法是索引10是日期和时间之间的间隔。
My thinking is that index 10 is the space between date and time.
有人可以帮我吗?我已经花了几个小时了:(
Could anyone help me with this? I've been at it for hours :(
推荐答案
格式错误导致问题。请请参考 ISO日期时间的格式为 2011-12-03T10:15:30。以下内容将为您提供想法
There is a error in the format of the that causes the issue. Please refer https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html.The ISO date time is of the format '2011-12-03T10:15:30' . The following will give you the idea
public static void main(String[] args) throws Exception {
String isoDate = "2016-07-09T01:30:00.0";
// ISO Local Date and Time '2011-12-03T10:15:30'
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTimeParked = LocalDateTime.parse(isoDate, formatter);
System.out.println(dateTimeParked);
String date = "2016-07-09 01:30:00.0";
DateTimeFormatter formatterNew = DateTimeFormatter.ofPattern("yyyy-LL-dd HH:mm:ss.S");
LocalDateTime dateTimeParkedNew = LocalDateTime.parse(date, formatterNew);
System.out.println(dateTimeParkedNew);
}
此打印件:
2016-07-09T01 :30
2016-07-09T01:30
This prints :2016-07-09T01:302016-07-09T01:30
这篇关于使用结果集将字符串变量从数据库(日期时间类型)解析为LocalDateTime变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!