Java Program to create Duration from seconds



We will get seconds here based on days, hours, millis and minutes, for example, how many seconds in 10 days, seconds in 10 hours, etc.

Duration for days, hours, milliseconds and minutes:

Duration duration = Duration.ofDays(10);
Duration duration1 = Duration.ofHours(10);
Duration duration2 = Duration.ofMillis(10);
Duration duration3 = Duration.ofMinutes(10);

Now, get the seconds:

System.out.println("Seconds in 10 days = "+duration.getSeconds());
System.out.println("Seconds in 10 hours = "+duration1.getSeconds());
System.out.println("Seconds in 10 milliseconds = "+duration2.getSeconds());
System.out.println("Seconds in 10 minutes = "+duration3.getSeconds());

Example

import java.time.Duration;
public class Demo {
   public static void main(String[] args) {
      Duration duration = Duration.ofDays(10);
      Duration duration1 = Duration.ofHours(10);
      Duration duration2 = Duration.ofMillis(10);
      Duration duration3 = Duration.ofMinutes(10);
      System.out.println("Seconds in 10 days = "+duration.getSeconds());
      System.out.println("Seconds in 10 hours = "+duration1.getSeconds());
      System.out.println("Seconds in 10 milliseconds = "+duration2.getSeconds());
      System.out.println("Seconds in 10 minutes = "+duration3.getSeconds());
   }
}

Output

Seconds in 10 days = 864000
Seconds in 10 hours = 36000
Seconds in 10 milliseconds = 0
Seconds in 10 minutes = 600

Advertisements