When should you prefer Duration and when should you avoid it?

When using Java, the `Duration` class plays a crucial role in measuring time intervals. It's essential to understand when to leverage this class optimally and when to consider alternatives.
Duration, Java, time intervals, performance, best practices

    // Java example using Duration
    import java.time.Duration;
    import java.time.LocalTime;

    public class DurationExample {
        public static void main(String[] args) {
            LocalTime startTime = LocalTime.now();
            // Simulate a delay
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LocalTime endTime = LocalTime.now();

            // Calculate Duration
            Duration duration = Duration.between(startTime, endTime);
            System.out.println("Duration in seconds: " + duration.getSeconds());
        }
    }
    

Duration Java time intervals performance best practices