Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the instant class that is part of the Java dot time package.
Until now, we have seen different ways of representing the time using date, month, years, hours,
minutes and seconds.
All of these are nothing but a human readable format.
Instant is a class which is part of the Java Time package, which is used to represent the time in a
machine readable format.
Basically it represents the time as a huge number.
How do you create an object of instant?
There is a handy method called now to create an object of instant.
The now method represents the number of seconds from the first second of Jan zero one, 1970, which
is otherwise called as epoch to the current time of this machine.
Let's go ahead and explore using some of the code examples in this tutorial.
At the College Open.
What I'm going to do, I'm going to create a class called Instant Example.
And then I'm going to make this class executable by adding public static void main method.
There we go.
Let's create the instance of instant.
Not now that this is going to give you let me put this in presentation mode.
So this is going to give you the current time of this machine, but it is represented in the form of
seconds.
I'm going to do instant.
An instant.
But if I try to print it, basically it is going to print as if like a date and time.
Because the two string method of instance is going to give you the time in the form of human readable
format.
But behind the scenes it is going to represent the time in a big number.
I will explain you that in a moment.
For now, let's print this and check.
What does it print?
If you take a look at it, it printed the result in a human readable format.
It looks like it has a date and time, right?
Let's go ahead and explore some of the methods that are part of the instant object.
We do instant dot.
There are no methods like get date, get month and get time and stuff like that.
It just has two methods, which is nothing but get nano and get epoch second.
Let's go ahead and print this one and check the value.
Now copy this and put it inside the shirt.
Let's print this.
Well run.
This will check the result.
Now.
But take a look at it.
This is the one which represents the actual current machine's time.
So.
As I mentioned in the presentation, Jan first.
1970.
The skull epoch and each and every day has 86,400 seconds.
Okay.
Each and every day has 86,400 seconds.
The first second of Jan first, 1970, which represents the value as zero from that point to this point.
How many seconds that is this value.
So the current machine's time is 2018, zero 719, and the time is ten, 14 and four zero.
Right.
And then let's go ahead and check the instant dot off method.
There is a handy off method.
And I'm going to pass zero as a value.
They pass the zero as a value.
This is going to give me the time as this one.
Let's go ahead and check that because that is the first second which represents.
The epic.
Run this example.
Take a look at it.
It represents 19
0101000000Z.
So.
This is the starting of the epoch.
Now, let's say you have another instance.
Of instant, instant, instant.
1 or 2.
Instant dot.
If you want to find the difference between these two instant, this instant and this instant, there
is a handy method called duration, which we used for the local time and local date time.
Right?
You can use the same thing here.
Duration dot between.
Instant.
Comma, instant one.
This is going to give you the difference between them and it is going to give you the duration as a
response.
Now what we are going to do.
We're going to get the duration and represent that in nano form because there is not much difference
between these two time instances.
Right?
Because this is created at line number ten and this is created at line number 18.
The time difference between them is going to be very, very minimal.
So in order to show some value, I'm going to represent that in nanoseconds.
Gonna have difference here.
Let's run this example and check the result.
There you go.
It gave you the time difference between these two instances as a nanosecond.
So this is all about instant, which you have to know.
The fundamental thing is instant is used to represent the time in a machine readable format.
With this, we came to the end of this tutorial.