I have time stamp in format 2017-18-08 11:45:30.345
.
I want to convert it to epoch time, so I am doing below:
String timeDateStr = \"2017
Your code will fail for below 3 reasons.
2017-18-08 12:60:30.345
), doesn't match with the Formatter you used. It should be yyyy-MM-dd HH:mm:ss.SSS instead of yyyy-dd-MM hh:mm:ss.SSSZonedDateTime
. So you would need to create a LocalDateTime
before and then pass a ZoneId
to it.The code should look like below:
String timeDateStr = "2017-18-08 12:59:30.345";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse(timeDateStr, dtf);
ZonedDateTime zdt = date.atZone(ZoneId.of("Europe/London"));
System.out.println(zdt.toInstant().toEpochMilli());