将日期解析为 yyyy-MM-dd HH:mm:ss 格式
Parse date to yyyy-MM-dd HH:mm:ss format
我想使用 LocalDateTime.parse() 方法解析日期格式为 yyyy-MM-dd HH:mm:ss 的日期,但我实际得到的是格式为 yyyy-MM-ddT 的日期HH:mm:ss。我不需要那个 'T'。请看下面的代码
LocalDateTime.parse("2016-10-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
我得到的结果是 2016-10-31T23:59:59。看到 'T'。 'T' 引起了问题,因此我无法将其保存到我的数据库中。我尝试将值保存在 datetime
类型的列中;我收到错误 - org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar
.
查看有效的值
VALUES('US','101','test','firstname','middleName','preferedName','lastName',
'1989-01-01','M',1,'11221123','test@test.com','address1','address2','Bloomingdale','IL','US','689850',
1,1,'11111','2016-12-31 23:59:59')
(最后一个值中没有 T
)
哪个不工作:
VALUES('US','101','test','firstname','middleName','preferedName','lastName',
'1989-01-01','M',1,'11221123','test@test.com','address1','address2','Bloomingdale','IL','US','689850',
1,1,'11111','2016-12-31T23:59:59')
(最后一个值的 T
)。
LocalDateTime 不是存储为字符串,而是存储为对象。
您得到的是 "T" 因为 .toString() 方法 return 默认为 ISO 格式,如下所述:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
toString
public String toString()
Outputs this date-time as a String, such as 2007-12-03T10:15:30.
The output will be one of the following ISO-8601 formats:
uuuu-MM-dd'T'HH:mm
uuuu-MM-dd'T'HH:mm:ss
uuuu-MM-dd'T'HH:mm:ss.SSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS
The Format used will be the shortest that outputs the full value of the
time where the omitted parts are implied to be zero.
根据需要使用格式函数输出。
LocalDateTime d = LocalDateTime.parse("2016-10-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("toString: " + d.toString());
//toString: 2016-10-31T23:59:59
System.out.println("format: " + d.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//format: 2016-10-31 23:59:59
我想使用 LocalDateTime.parse() 方法解析日期格式为 yyyy-MM-dd HH:mm:ss 的日期,但我实际得到的是格式为 yyyy-MM-ddT 的日期HH:mm:ss。我不需要那个 'T'。请看下面的代码
LocalDateTime.parse("2016-10-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
我得到的结果是 2016-10-31T23:59:59。看到 'T'。 'T' 引起了问题,因此我无法将其保存到我的数据库中。我尝试将值保存在 datetime
类型的列中;我收到错误 - org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar
.
查看有效的值
VALUES('US','101','test','firstname','middleName','preferedName','lastName',
'1989-01-01','M',1,'11221123','test@test.com','address1','address2','Bloomingdale','IL','US','689850',
1,1,'11111','2016-12-31 23:59:59')
(最后一个值中没有 T
)
哪个不工作:
VALUES('US','101','test','firstname','middleName','preferedName','lastName',
'1989-01-01','M',1,'11221123','test@test.com','address1','address2','Bloomingdale','IL','US','689850',
1,1,'11111','2016-12-31T23:59:59')
(最后一个值的 T
)。
LocalDateTime 不是存储为字符串,而是存储为对象。
您得到的是 "T" 因为 .toString() 方法 return 默认为 ISO 格式,如下所述: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
toString
public String toString()
Outputs this date-time as a String, such as 2007-12-03T10:15:30.
The output will be one of the following ISO-8601 formats:
uuuu-MM-dd'T'HH:mm
uuuu-MM-dd'T'HH:mm:ss
uuuu-MM-dd'T'HH:mm:ss.SSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSThe Format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
根据需要使用格式函数输出。
LocalDateTime d = LocalDateTime.parse("2016-10-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("toString: " + d.toString());
//toString: 2016-10-31T23:59:59
System.out.println("format: " + d.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//format: 2016-10-31 23:59:59