Java Date Time - Duration toHours() example








Duration toHours() gets the number of hours in this duration.

This returns the total number of hours in the duration by dividing the number of seconds by 3600.

Syntax

toHours has the following syntax.

public long toHours()

Example

The following example shows how to use toHours.

import java.time.Duration;
import java.time.LocalTime;
//from w w w .jav a2  s .c  o m
public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
    System.out.println(duration.toHours());
    
  }
}

The code above generates the following result.