在Java中,我有一个表示日期/时间的字符串为“ 2018-07-13T15:36:20.000”,我需要使用DateTimeFormatter将其转换为Timestamp对象-有人知道怎么做吗?我很难找到所需的DateTimeFormatter常量。
您可以使用ISO_LOCAL_DATE_TIME DateTimeFormatter:
public static final DateTimeFormatter ISO_LOCAL_DATE_TIME
ISO日期时间格式化程序,用于格式化或解析日期时间而无需偏移量,例如'2011-12-03T10:15:30'。
[DateTimeFormatter
无法用于创建java.sql.Timestamp
对象。
使用LocalDateTime
很容易做到:
Timestamp ts = Timestamp.valueOf(LocalDateTime.parse("2018-07-13T15:36:20.000"));
System.out.println(ts);
输出
2018-07-13 15:36:20.0