I'm using java.time to convert date time to unix time but the result is not always correct when I have different input formats. In the code below the output is not correct. It converts "2016-06-21-10-19-22" to 2016-06-21T00:00:00+00:00.
If the input is "04/28/2016", "MM/dd/yyyy", the result is correct.
I want to know how to write a function that can convert both date with time and date without time to the correct timeInSeconds?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy-MM-dd-HH-mm-ss"); String dateTimeString = "2016-06-21-10-19-22"; LocalDate date = LocalDate.parse(dateTimeString, formatter); ZonedDateTime resultado = date.atStartOfDay(ZoneId.of("UTC")); Instant i = resultado.toInstant(); long timeInSeconds = i.getEpochSecond(); int nanoAdjustment = i.getNano(); System.out.println("" + timeInSeconds + " seconds " + nanoAdjustment + " nanoseconds");//1466467200 seconds 0 nanoseconds 