I'm new with the java8 time API, so pardon the poor formulation.
What I'm looking to do is figuring out when it's the next time (starting from a timestamp) a certain criterion is met, and when it stops being met.
For an example let's say I have a millisecond timestamp and I want to figure out what is the next timestamp that is also a Friday at a given timezone.
I also need to figure out when it stops being Friday. As the start might eg. be 4:20 friday afternoon (if that's what the time is) so start + length of unit is not enough.
Obv lots of dirty ways doing this, but I'm looking for an elegant and java8-esque solution. The actual problem I'm facing is a bit more complicated than this, but I think solving the Friday example gives me enough clues to get forward.
First off, you probably want to use
Instant is a count of milliseconds since the epoch, and doesn't really support concepts like "date" or "time" or "day of week".
There isn't really a utility for "find date matching criteria" - afaik, you're going to have to do it yourself.
final Instant startVal = Instant.now(); final ZonedDateTime date = startVal.atZone(ZoneOffset.UTC); // or whatever final int daysUntilFriday = (DayOfWeek.FRIDAY.getValue() - date.getDayOfWeek().getValue() + 7) % 7; // +7 to avoid a negative value final ZonedDateTime startOfNextFriday = date.plusDays(daysUntilFriday).truncatedTo(ChronoUnit.DAYS); final ZonedDateTime endOfNextFriday = ZonedDateTime.of(startOfNextFriday.toLocalDate(), LocalTime.MAX, startOfNextFriday.getZone());