LocalDateTime parse to epoch seconds

Some Name :

I'm trying to parse a string of the following form 2010-10-12-18-43-55 to seconds since epoch in the current timezone. Here is what I tried

DateTimeFormatter dateTimeFormatter = 
          DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");

I.

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

II.

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

III.

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

Can someone help me to solve this task?

Robert Bain :
  1. From the Instant javadocs the String has to be in the format 2007-12-03T10:15:30.00Z.
  2. The String can't be parsed to an Instant because the timezone is missing. Try this: ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
  3. I'm not sure what you're trying to do.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159590&siteId=1