I'm using a Java DateTimeFormatter
in order to parse dates and datetimes. I construct it with a DateTimeFormatterBuilder
dtfb
, and then try to get epoch seconds from it as follows:
dtfb.toFormatter().withZone(ZoneId.of("America/New_York"))
.parse(timeStr)
.getLong(ChronoField.INSTANT_SECONDS)
However, this only works if timeStr
is specific enough to identify one particular second. For example, this works: "1997.10.10 23:45:23"
, but this does not "1997.10.10"
, as there's no hour minute second information. I'm wondering if there's a way in Java to systematically "round down" and return the first timestamp that fits the constraints. For example, in this failing case I'd want the answer to be the timestamp at "1997.10.10 00:00:00"
. Basically just keep filling in zeros in smaller and smaller time units until it's specific enough to give me a second. Is this possible?