Joda Time Instant by Examples in Java

Tags: Joda Time Instant DateTime Duration Date MutableDateTime DateTimeFieldType Joda Instant Joda DateTime Joda Duration Joda MutableDateTime Joda DateTimeFieldType

In this Java tutorial, we learn how to use the org.joda.time.Instant class of Joda Time library by different example Java programs.

Add Joda Time library to your Java project

To use Joda Time Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'joda-time', name: 'joda-time', version: '2.10.9'

To use Joda Time Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.9</version>
</dependency>

To download the Joda Time .jar file you can visit Joda Time releases page at github.com/JodaOrg/joda-time

Create new Instant objects

Using default constructor to create a new Instant object represents the current system millisecond time.

JodaTimeInstantExample1.java

import org.joda.time.Instant;

public class JodaTimeInstantExample1 {
    public static void main(String[] args) {

        Instant instant = new Instant();

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
1610560316348
2021-01-13T17:51:56.348Z

Using Instant.now() static method to create a new Instant object represents the current system millisecond time.

JodaTimeInstantExample2.java

import org.joda.time.Instant;

public class JodaTimeInstantExample2 {
    public static void main(String[] args) {

        Instant instant = Instant.now();

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
1610560353462
2021-01-13T17:52:33.462Z

Create an Instant object with given milliseconds from 1970-01-01T00:00:00Z.

JodaTimeInstantExample3.java

import org.joda.time.Instant;

public class JodaTimeInstantExample3 {
    public static void main(String[] args) {

        Instant instant = new Instant(8000);

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
8000
1970-01-01T00:00:08.000Z

Create an Instant object with a given object that represents a datetime.

JodaTimeInstantExample4.java

import org.joda.time.DateTime;
import org.joda.time.Instant;

public class JodaTimeInstantExample4 {
    public static void main(String[] args) {

        DateTime dateTime = new DateTime();
        Instant instant = new Instant(dateTime);

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
1610560433833
2021-01-13T17:53:53.833Z

Create an Instant object with a given datetime string.

JodaTimeInstantExample5.java

import org.joda.time.Instant;

public class JodaTimeInstantExample5 {
    public static void main(String[] args) {

        Instant instant = new Instant("2021-01-01T10:10:10.064Z");

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
1609495810064
2021-01-01T10:10:10.064Z

Using Instant.parse() static method to parse String into Instant object

JodaTimeInstantExample6.java

import org.joda.time.Instant;

public class JodaTimeInstantExample6 {
    public static void main(String[] args) {

        Instant instant = Instant.parse("2021-01-01T10:10:10.064Z");

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
1609495810064
2021-01-01T10:10:10.064Z

How to use Instant.ofEpochMilli() static method

The following Java example using Instant.ofEpochMilli() static method to create a new Instant object with given milliseconds from 1970-01-01T00:00:00Z

JodaTimeInstantExample7.java

import org.joda.time.Instant;

public class JodaTimeInstantExample7 {
    public static void main(String[] args) {

        Instant instant = Instant.ofEpochMilli(2000);

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
2000
1970-01-01T00:00:02.000Z

How to use Instant.ofEpochSecond() static method

In the following Java example we use the Instant.ofEpochSecond() static method to create a new Instant object with given seconds from 1970-01-01T00:00:00Z

JodaTimeInstantExample8.java

import org.joda.time.Instant;

public class JodaTimeInstantExample8 {
    public static void main(String[] args) {

        Instant instant = Instant.ofEpochSecond(7);

        System.out.println(instant.getMillis());
        System.out.println(instant);
    }
}
The output is:
7000
1970-01-01T00:00:07.000Z

How to use withDurationAdded() method

The following Java example to show how to use the withDurationAdded() to get a new Instant from a current Instant object with adding a given milliseconds.

JodaTimeInstantExample9.java

import org.joda.time.Instant;

public class JodaTimeInstantExample9 {
    public static void main(String[] args) {
        Instant instant1 = new Instant();

        Instant instant2 = instant1.withDurationAdded(1000, 10); // 1000 x 10 = 10000 milliseconds = 10 seconds

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T17:58:44.159Z
2021-01-13T17:58:54.159Z

Using withDurationAdded() with given Duration object

JodaTimeInstantExample10.java

import org.joda.time.Duration;
import org.joda.time.Instant;

public class JodaTimeInstantExample10 {
    public static void main(String[] args) {
        Instant instant1 = new Instant();

        Duration oneSecondDuration = new Duration(1000);// 1000 milliseconds
        Instant instant2 = instant1.withDurationAdded(oneSecondDuration, 10);

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T17:59:02.065Z
2021-01-13T17:59:12.065Z

How to use withMillis() method

The following Java example we use withMillis() method to create a new Instant object with a given milliseconds.

JodaTimeInstantExample11.java

import org.joda.time.Instant;

public class JodaTimeInstantExample11 {
    public static void main(String[] args) {
        Instant instant1 = new Instant();

        Instant instant2 = instant1.withMillis(2000);

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T18:00:19.152Z
1970-01-01T00:00:02.000Z

How to use plus() method

The following Java example we use plus() method to return a new Instant object from current Instant with adding a specified milliseconds.

JodaTimeInstantExample12.java

import org.joda.time.Instant;

public class JodaTimeInstantExample12 {
    public static void main(String[] args) {
        Instant instant1 = new Instant();

        Instant instant2 = instant1.plus(1000);// 1000 milliseconds = 1 second

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T18:00:36.446Z
2021-01-13T18:00:37.446Z

The plus() method also can be used with a given Duration object.

JodaTimeInstantExample13.java

import org.joda.time.Duration;
import org.joda.time.Instant;

public class JodaTimeInstantExample13 {
    public static void main(String[] args) {
        Instant instant1 = new Instant();

        Duration oneMinuteDuration = new Duration(60000); // 60000 milliseconds
        Instant instant2 = instant1.plus(oneMinuteDuration);

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T18:00:55.752Z
2021-01-13T18:01:55.752Z

How to use minus() method

In the following Java example program we use minus() method to return an new Instant object from current Instant with subtracting a specified milliseconds.

JodaTimeInstantExample14.java

import org.joda.time.Instant;

public class JodaTimeInstantExample14 {
    public static void main(String[] args) {

        Instant instant1 = new Instant();

        Instant instant2 = instant1.minus(1000);// 1000 milliseconds = 1 second

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T18:02:18.159Z
2021-01-13T18:02:17.159Z

The minus() method also can be used with a given Duration object.

JodaTimeInstantExample15.java

import org.joda.time.Duration;
import org.joda.time.Instant;

public class JodaTimeInstantExample15 {
    public static void main(String[] args) {
        Instant instant1 = new Instant();

        Duration oneMinuteDuration = new Duration(60000); // 60000 milliseconds = 60 seconds = 1 minute
        Instant instant2 = instant1.minus(oneMinuteDuration);

        System.out.println(instant1);
        System.out.println(instant2);
    }
}
The output is:
2021-01-13T18:02:41.661Z
2021-01-13T18:01:41.661Z

Convert Instant to DateTime

JodaTimeInstantExample16.java

import org.joda.time.DateTime;
import org.joda.time.Instant;

public class JodaTimeInstantExample16 {
    public static void main(String[] args) {
        Instant instant = new Instant();

        DateTime dateTime = instant.toDateTime();

        System.out.println(dateTime);
    }
}
The output is:
2021-01-14T01:05:45.155+07:00

Convert Instant to Date

JodaTimeInstantExample17.java

import org.joda.time.Instant;

import java.util.Date;

public class JodaTimeInstantExample17 {
    public static void main(String[] args) {
        Instant instant = new Instant();

        Date date = instant.toDate();

        System.out.println(date);
    }
}
The output is:
Thu Jan 14 01:06:00 ICT 2021

Convert Instant to MutableDateTime

JodaTimeInstantExample18.java

import org.joda.time.Instant;
import org.joda.time.MutableDateTime;

public class JodaTimeInstantExample18 {
    public static void main(String[] args) {
        Instant instant = new Instant();

        MutableDateTime mutableDateTime = instant.toMutableDateTime();

        System.out.println(mutableDateTime);
    }
}
The output is:
2021-01-14T01:06:18.787+07:00

How to use get() method to get value of Datetime on an Instant object

JodaTimeInstantExample19.java

import org.joda.time.DateTimeFieldType;
import org.joda.time.Instant;

public class JodaTimeInstantExample19 {
    public static void main(String[] args) {
        Instant instant = new Instant();

        int dayOfMonth = instant.get(DateTimeFieldType.dayOfMonth());
        int monthOfYear = instant.get(DateTimeFieldType.monthOfYear());
        int year = instant.get(DateTimeFieldType.year());
        int hourOfDay = instant.get(DateTimeFieldType.hourOfDay());
        int minuteOfHour = instant.get(DateTimeFieldType.minuteOfHour());
        int secondOfMinute = instant.get(DateTimeFieldType.secondOfMinute());

        System.out.println(dayOfMonth);
        System.out.println(monthOfYear);
        System.out.println(year);
        System.out.println(hourOfDay);
        System.out.println(minuteOfHour);
        System.out.println(secondOfMinute);
    }
}
The output is:
13
1
2021
18
6
37

How to use Instant.compareTo() method

JodaTimeInstantExample20.java

import org.joda.time.Instant;

public class JodaTimeInstantExample20 {
    public static void main(String[] args) {
        Instant now = Instant.now();
        Instant oneSecondAgo = now.minus(1000);

        int compareResult = now.compareTo(oneSecondAgo);

        System.out.print(compareResult);
    }
}
The output is:
1

How to use Instant.isBefore() method

JodaTimeInstantExample21.java

import org.joda.time.Instant;

public class JodaTimeInstantExample21 {
    public static void main(String[] args) {
        Instant now = Instant.now();
        Instant oneMinuteAgo = now.minus(60000);

        System.out.println(now.isBefore(oneMinuteAgo));

        System.out.println(oneMinuteAgo.isBefore(now));

        System.out.println(now.isBefore(now));
    }
}
The output is:
false
true
false

How to use Instant.isBeforeNow() method

JodaTimeInstantExample22.java

import org.joda.time.Instant;

public class JodaTimeInstantExample22 {
    public static void main(String[] args) {
        Instant instant = Instant.now();

        System.out.println(instant.isBeforeNow());
    }
}
The output is:
false

How to use Instant.isAfter() method

JodaTimeInstantExample23.java

import org.joda.time.Instant;

public class JodaTimeInstantExample23 {
    public static void main(String[] args) {
        Instant now = Instant.now();
        Instant oneMinuteAgo = now.minus(60000);

        System.out.println(now.isAfter(oneMinuteAgo));

        System.out.println(oneMinuteAgo.isAfter(now));

        System.out.println(now.isAfter(now));
    }
}
The output is:
true
false
false

How to use Instant.isAfterNow() method

JodaTimeInstantExample24.java

import org.joda.time.Instant;

public class JodaTimeInstantExample24 {
    public static void main(String[] args) {
        Instant instant = Instant.now();

        System.out.println(instant.isAfterNow());
    }
}
The output is:
false

How to use Instant.isEqual() method

JodaTimeInstantExample25.java

import org.joda.time.Instant;

public class JodaTimeInstantExample25 {
    public static void main(String[] args) {
        Instant now = Instant.now();
        Instant oneMinuteAgo = now.minus(60000);

        System.out.println(now.isEqual(oneMinuteAgo));

        System.out.println(oneMinuteAgo.isEqual(now));

        System.out.println(now.isEqual(now));
    }
}
The output is:
false
false
true

How to use Instant.isEqualNow() method

JodaTimeInstantExample26.java

import org.joda.time.Instant;

public class JodaTimeInstantExample26 {
    public static void main(String[] args) {
        Instant instant = Instant.now();

        System.out.println(instant.isEqualNow());
    }
}
The output is:
true

Happy Coding 😊

Joda Time LocalDate by Examples in Java

Joda Time LocalTime by Examples in Java

Joda Time LocalDateTime by Examples in Java

Joda Time DateTime by Examples in Java

Joda Time Duration by Examples in Java

Joda Time Period by Examples in Java

Joda Time Interval by Examples in Java