【发布时间】:2019-07-24 13:30:50
【问题描述】:
我正在尝试将以下形式的字符串 2010-10-12-18-43-55 解析为当前时区中纪元以来的秒数。这是我尝试过的
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
我。
String datatetime = "2010-10-12-18-43-55";
Instant.parse(datetime).toEpochMilli;
//Throws
//java.time.format.DateTimeParseException:
//Text '2010-10-12-18-43-55' could not be parsed at index 10
二。
String datatetime = "2010-10-12-18-43-55";
Instant.from(LocalDateTime.parse(datetime, dateTimeFormatter)).toEpochMilli
//Throws
//java.time.DateTimeException: Unable to obtain Instant from
//TemporalAccessor: 2010-10-12T18:43:55 of type
//java.time.LocalDateTime type java.time.LocalDateTime
三。
String datatetime = "2010-10-12-18-43-55";
Instant.from(dateTimeFormatter.parse(datetime, new ParsePosition(0))).toEpochMilli
//Throws
//java.time.DateTimeException: Unable to obtain Instant from
//TemporalAccessor: {},ISO resolved to 2010-10-12T18:43:55
//of type java.time.format.Parsed
有人可以帮我解决这个任务吗?
【问题讨论】:
-
你确定那是 java:
String datatetime = 2010-10-12-18-43-55;吗?缺少引号,是吗? -
@CarlosHeuberger 谢谢,已修复。
-
你可以使用
Instant inst = LocalDateTime.parse(datetime, dateTimeFormatter).toInstant(ZoneOffset.UTC);修复尝试II