JodaTime Instant

Overview

An
Instant
represents a point in time independent of Chronology or TimeZone.

  1. Creating Instant
  2. Comparing Two Instants
  3. Get Sub-Fields of Instant
  4. Perform Arithmentic Operations On Instant like Addition, Subtraction and Repeated Addition
  5. Convert Instant To Other Representations Like DateTime, MutableDateTime,JDK Date and String

Creating Instant

Instant
can be created in the following ways:

  • Using null argument to create Instant for current DateTime
  • Using Instant.now()to create Instant for current DateTime
  • Using millis
  • Using another Instant
  • Parsing from String representation


Comparing Two Instants

There are two ways to compare Instants:

  1. Using Instant.compareTo() method.
    The return type is an integer whose values follow the contract mentioned in Comparable interface
  2. Using convenience methods line Instant.isBefore(),Instant.isAfter() and Instant.isEqual()
    The return type in this case is boolean.

The first approach is typically used in comparator logic for sorting.


Get Sub-Fields of Instant

This example shows how to extract sub fields of an Instant.

In this example, only year, month and day have been shown. However any sub-field can be derived in a similar manner


Perform Arithmentic Operations On Instant like Addition, Subtraction and Repeated Addition

The following types of arithmetic operations are supported in Joda-Time:

  • Addition
  • Subtraction
  • Repeated Addition


Convert Instant To Other Representations Like DateTime, MutableDateTime,JDK Date and String


To start, click on the next arrow. If you find anything missing in the tutorials then please drop in your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *