java.time.Duration Usage Examples

This section provides a tutorial example on how to use the java.time.Duration class to capture and apply duration of time, and to retrieve days, hours, minutes, seconds and nanoseconds values.

To test out the java.time.Duration class, I wrote the following example program,

 - Copyright (c) 2014,, All Rights Reserved.
import java.time.Duration;
import java.time.*;
import java.time.temporal.*;
class DurationTest {
   public static void main(String[] a) { out = System.out;

      Duration d11 = Duration.ofSeconds(1,1);
      out.println("toString() = " + d11.toString());
      out.println("getSeconds() = " + d11.getSeconds());
      out.println("getNano() = " + d11.getNano());
      out.println("toHours() = " + d11.toHours());
      out.println("toMinutes() = " + d11.toMinutes());
      out.println("get(SECONDS) = " 
         + d11.get(java.time.temporal.ChronoUnit.SECONDS));

      Duration d1011 = d11.plusHours(1);
      out.println("plusHours() method:");
      out.println("toString() = " + d1011.toString());
      out.println("getSeconds() = " + d1011.getSeconds());
      out.println("toHours() = " + d1011.toHours());
      out.println("toMinutes() = " + d1011.toMinutes());

      Duration d24h = Duration.ofHours(24);

      out.println("Daylight saving time change test 1:");
      ZonedDateTime time1 = ZonedDateTime.parse(
      out.println("Time 1 before addTo() = " + time1.toString());
      time1 = (ZonedDateTime) d24h.addTo(time1);
      out.println("Time 1 after addTo() = " + time1.toString());

      out.println("Daylight saving time change test 2:");
      ZonedDateTime time2 = ZonedDateTime.parse(
      out.println("Time 2 before addTo() = " + time2.toString());
      time2 = (ZonedDateTime) d24h.addTo(time2);
      out.println("Time 2 after addTo() = " + time2.toString());

When running with JDK 1.8, I got the following output:

toString() = PT1.000000001S
getSeconds() = 1
getNano() = 1
toHours() = 0
toMinutes() = 0
get(SECONDS) = 1

plusHours() method:
toString() = PT1H1.000000001S
getSeconds() = 3601
toHours() = 1
toMinutes() = 60

Daylight saving time change test 1:
Time 1 before addTo() = 2014-03-08T02:45-05:00[America/Montreal]
Time 1 after addTo() = 2014-03-09T03:45-04:00[America/Montreal]

Daylight saving time change test 2:
Time 2 before addTo() = 2014-03-08T03:05-05:00[America/Montreal]
Time 2 after addTo() = 2014-03-09T04:05-04:00[America/Montreal]

Interesting notes from the output:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

Java Date-Time API

 What Is Date-Time API?

 java.time.Instant - Representing a Moment of Time

 java.time.Instant Usage Examples

 java.time.Instant - Get(INSTANT_SECONDS) Error

 Converting java.util.Date to java.time.Instant

 java.time.ZonedDateTime - Calendar and Timezone

 java.time.ZonedDateTime Usage Examples

 Converting java.util.GregorianCalendar to java.time.ZonedDateTime

 java.time.OffsetDateTime - Calendar and UTC Offset

 java.time.OffsetDateTime Usage Examples

 java.time.LocalDateTime - Local Date and time without Timezone

 Partial Date and Time Objects and Classes

 java.time.format.DateTimeFormatter - Date-Time Strings

 java.time.Duration - Time Durations

java.time.Duration Usage Examples

 java.time.Period - Periods in Days and Months

 java.time.Period Usage Examples

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version