Skip to content

Instantly share code, notes, and snippets.

@sorokod
Last active December 12, 2022 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorokod/69c082e94c2966e5eb339bac89d424b5 to your computer and use it in GitHub Desktop.
Save sorokod/69c082e94c2966e5eb339bac89d424b5 to your computer and use it in GitHub Desktop.
import org.springframework.scheduling.support.CronExpression
import java.time.LocalDateTime
import java.time.LocalDateTime.now
/**
* Let Spring generate a sequence of dates according top the provided cron string.
* See: https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
*/
fun genTimes(cronStr: String): Sequence<LocalDateTime> =
CronExpression.parse(cronStr).let { exp ->
generateSequence(now()) { t -> exp.next(t) }
}
fun main() {
genTimes("0 * * * * *")
.take(10)
.forEach { println(it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment