• In this quick tutorial, we’re going to take a look at two new classes for working with dates introduced in Java 8: Period and Duration .
  • java.lang.Object. java.time.Duration. ... A physical duration could be of infinite length. For practicality, the duration is stored with constraints similar to Instant.
  • Yayın zamanı: önceki gün
    The ofDays() method in Java, part of the java.time.Duration class, is used to create a Duration instance representing a specified number of days.
  • Java 9 is introducing few new methods in Duration class. Before we jump into the details let’s have a quick recap what Duration is.
  • java.time.Duration Tutorial with Examples. Duration class represents a time-based amount of time between two Instant objects, such as '25.5 seconds'.
  • The Duration class inherits an object class ( as an object is the superclass of all classes in java) and implements the Comparable interface.
  • The java.time.Duration class is part of new date and time API added in Java 8 that represents a time-based amount of time, such as '34.5 seconds'.
  • Duration is the value-based Class present in the Java time library. It’s used to get time-based amount of time. This class is immutable and thread-safe.
  • In this example, we import the java.time.Duration class and use the ofHours method to create a duration of 2 hours.
  • Few examples to show you how to use Java 8 Duration , Period and ChronoUnit objects to find out the difference between dates.