如何使用Groovy将DATETIME
类型的数据从2018-12-14 14:16:58.037967
格式化为2018-12-14T14:16:58.037967
?
程序:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
def DB_formattime = LocalDateTime.parse("2019-05-18 20:54:04.638314", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.n"))
print DB_formattime
错误:
Caught: java.time.format.DateTimeParseException: Text '2019-05-18 20:54:04.638314' could not be parsed at index 10
java.time.format.DateTimeParseException: Text '2019-05-18 20:54:04.638314' could not be parsed at index 10
at java_time_LocalDateTime$parse.call(Unknown Source)
at test.run(test.groovy:4)
Process finished with exit code 1
答案 0 :(得分:1)
这里最简单的方法就是用'T'字符代替空格...
"2018-12-14 14:16:58.037967".tr(' ','T')
但是,如果您想解析和格式化日期,则:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
def DB_formattime = LocalDateTime.parse("2019-05-18 20:54:04.638314", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n"))
DB_formattime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.n"))