Duration and Period in Java: Differences and Examples
Duration measures time in seconds and nanoseconds, Period measures dates in years, months and days. Compare them with runnable Java examples.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Duration measures time in seconds and nanoseconds, Period measures dates in years, months and days. Compare them with runnable Java examples.
Use ZonedDateTime.withZoneSameInstant with region ZoneId values to convert a moment between zones. Covers DST gaps, LocalDateTime input and storing UTC.
ZoneId identifies a time zone in java.time and carries its daylight saving rules. See how to create it, convert times and avoid common mistakes.
Add and subtract days, months and years, measure the gap between two dates and handle time zones in Java using LocalDate, Period, Duration and ChronoUnit.
LocalDate, LocalTime and LocalDateTime are immutable java.time classes without a time zone. See how to create, format, compare and convert them with examples.
Learn why java.time replaced Date and Calendar, which class to pick, and how to format, parse, calculate and convert dates safely in Java.
Use DateTimeFormatter with LocalDate, LocalDateTime or ZonedDateTime to format dates in Java, and avoid the legacy SimpleDateFormat.
Schedule delayed and repeating tasks with java.util.Timer, understand its single-thread and exception pitfalls, and move to ScheduledExecutorService.
Runnable returns nothing, Callable returns a result and may throw checked exceptions. See both used with ExecutorService, Future and virtual threads.
Use ScheduledExecutorService to run Java tasks after a delay or on a repeating schedule, with fixed-rate or fixed-delay timing, cancellation and safe shutdown.
ScheduledExecutorService schedules one-off and repeating tasks on a thread pool. Learn fixed rate vs fixed delay, cancellation, shutdown and exception pitfalls.
Backpressure lets a slow subscriber control a fast publisher. See how request(n) works and which Reactor operators to use when a source cannot slow down.
Mono emits zero or one item and Flux emits zero to many. Learn how they differ, the core operators and how to use them in Spring WebFlux.
Project Reactor models async data as Mono and Flux pipelines that run when subscribed. Learn operators, schedulers, WebFlux usage and testing with StepVerifier.
Reactive programming builds non-blocking pipelines with backpressure. See the Flow API, Reactor examples and when to prefer virtual threads.
ForkJoinPool splits large CPU-bound jobs into subtasks and balances them across threads by work stealing. Includes a full RecursiveTask example.
Use exceptionally for fallbacks, handle for both outcomes and whenComplete for side effects. Includes CompletionException unwrapping and timeouts.
CompletableFuture lets you run Java code asynchronously and compose the results. See chaining, combining, error handling, timeouts and executor choices.
Use Optional as a return type to make missing values explicit, then unwrap it with map, orElseGet and orElseThrow instead of null checks.
Optional is a Java container that holds a value or is empty, making missing results explicit. Learn creation, orElse, map, flatMap and what to avoid.