Understanding Instant in Java

In this post I am going to show you EXACTLY how to work with Instant class in Java.

Instant class is a part of java.time package.

Instant class is designed to represent specific point in time with nanosecond precision in coordinated universal time (UTC).

It does not depends on any time zone

Creating Instant

To create an instant class you can use Instant.now() method to obtain current timestamp without any timezone information

Let us check with an example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.time.Instant;

public class InstantDemo {


    public static void main(String[] args) {

        Instant instant = Instant.now();
        System.out.println(instant);


    }
}

Output is:

1
2023-11-18T11:05:26.570704700Z

From the above output you can see it does not depends upon any timezone

Creating Instant from ofEpochSecond method

You can create Instant from Unix timestamp which is nothing but number of seconds since Unix epoch, January 1, 1970

In the below example we have number no of seconds after January 1,1970

We can use Instant.ofEpochSecond() method and pass number of seconds as arguments to get Instant object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.time.Instant;

public class InstantDemo {


    public static void main(String[] args) {

        Instant instant = Instant.ofEpochSecond(1423459200L);
        System.out.println(instant);


    }
}

Output is:

1
2015-02-09T05:20:00Z

Creating Instant using string in ISO-8601 format.

Instant.parse() is used to create an Instant object from a string representation of a timestamp. This string must be in the ISO-8601 format.

If the string is not in the correct format, a DateTimeParseException will be thrown.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.time.Instant;


public class InstantDemo {


    public static void main(String[] args) {

        Instant instant = Instant.parse("2023-11-17T11:35:30.00Z");
        System.out.println(instant);

    }
}

Manipulating Instant

You can perform operations like adding and subtracting time using plus and minus method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.time.Duration;
import java.time.Instant;


public class InstantDemo {


    public static void main(String[] args) {

        Instant instant = Instant.parse("2023-11-17T00:00:00.00Z");
        System.out.println(instant.plus(Duration.ofDays(10)));
        System.out.println(instant.plus(Duration.ofHours(10)));

    }
}
1
2
2023-11-27T00:00:00Z
2023-11-17T10:00:00Z

Converting Instant to ZonedDateTime

You can combine Instant with a timezone to get instant at specific timezone. In the below example we have converted Instant to “Asia/Kolkata” timezone using atZone method of Instant class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;


public class InstantDemo {


    public static void main(String[] args) {

        Instant instant = Instant.parse("2023-11-17T00:00:00.00Z");
        System.out.println(instant);
        System.out.println("Instant with timezone: "+instant.atZone(ZoneId.of("Asia/Kolkata")));

    }
}
1
2
2023-11-17T00:00:00Z
Instant with timezone: 2023-11-17T05:30+05:30[Asia/Kolkata]

Conclusion

Thus,Java’s instant class can be used for handling timestamps and managing time-related operations with precision. It can be used in wide range of applications like logging and measuring execution times for it’s accuracy.