如何在Java中使用DateTimeFormatter从字符串制作时间戳

问题描述 投票:0回答:2

在Java中,我有一个表示日期/时间的字符串为“ 2018-07-13T15:36:20.000”,我需要使用DateTimeFormatter将其转换为Timestamp对象-有人知道怎么做吗?我很难找到所需的DateTimeFormatter常量。

java datetime timestamp datetime-format
2个回答
0
投票

您可以使用ISO_LOCAL_DATE_TIME DateTimeFormatter

public static final DateTimeFormatter ISO_LOCAL_DATE_TIME

ISO日期时间格式化程序,用于格式化或解析日期时间而无需偏移量,例如'2011-12-03T10:15:30'。


0
投票

[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
© www.soinside.com 2019 - 2024. All rights reserved.