WEBVTT

00:01.430 --> 00:02.030
Hi everyone.

00:02.030 --> 00:03.380
Welcome back to this tutorial.

00:03.380 --> 00:09.920
In this tutorial we will code and explore the instant class that is part of the Java dot time package.

00:09.920 --> 00:18.380
Until now, we have seen different ways of representing the time using date, month, years, hours,

00:18.410 --> 00:20.180
minutes and seconds.

00:20.210 --> 00:24.200
All of these are nothing but a human readable format.

00:24.230 --> 00:31.520
Instant is a class which is part of the Java Time package, which is used to represent the time in a

00:31.520 --> 00:33.500
machine readable format.

00:33.530 --> 00:37.430
Basically it represents the time as a huge number.

00:37.550 --> 00:39.950
How do you create an object of instant?

00:40.430 --> 00:46.010
There is a handy method called now to create an object of instant.

00:46.040 --> 00:54.530
The now method represents the number of seconds from the first second of Jan zero one, 1970, which

00:54.530 --> 00:58.970
is otherwise called as epoch to the current time of this machine.

00:58.970 --> 01:03.720
Let's go ahead and explore using some of the code examples in this tutorial.

01:04.330 --> 01:05.650
At the College Open.

01:05.660 --> 01:10.460
What I'm going to do, I'm going to create a class called Instant Example.

01:13.010 --> 01:17.330
And then I'm going to make this class executable by adding public static void main method.

01:18.170 --> 01:18.800
There we go.

01:18.830 --> 01:22.130
Let's create the instance of instant.

01:23.370 --> 01:28.290
Not now that this is going to give you let me put this in presentation mode.

01:30.910 --> 01:37.930
So this is going to give you the current time of this machine, but it is represented in the form of

01:37.930 --> 01:38.710
seconds.

01:39.370 --> 01:40.690
I'm going to do instant.

01:42.230 --> 01:43.130
An instant.

01:44.720 --> 01:50.480
But if I try to print it, basically it is going to print as if like a date and time.

01:51.770 --> 01:58.940
Because the two string method of instance is going to give you the time in the form of human readable

01:58.940 --> 01:59.390
format.

01:59.390 --> 02:03.980
But behind the scenes it is going to represent the time in a big number.

02:03.980 --> 02:05.450
I will explain you that in a moment.

02:05.450 --> 02:07.070
For now, let's print this and check.

02:07.070 --> 02:07.970
What does it print?

02:11.650 --> 02:15.130
If you take a look at it, it printed the result in a human readable format.

02:15.160 --> 02:17.620
It looks like it has a date and time, right?

02:17.650 --> 02:22.120
Let's go ahead and explore some of the methods that are part of the instant object.

02:22.960 --> 02:24.850
We do instant dot.

02:25.950 --> 02:32.820
There are no methods like get date, get month and get time and stuff like that.

02:32.820 --> 02:37.680
It just has two methods, which is nothing but get nano and get epoch second.

02:37.710 --> 02:40.260
Let's go ahead and print this one and check the value.

02:44.330 --> 02:47.780
Now copy this and put it inside the shirt.

02:48.350 --> 02:49.220
Let's print this.

02:52.730 --> 02:53.100
Well run.

02:53.150 --> 02:54.890
This will check the result.

02:54.920 --> 02:55.310
Now.

02:56.770 --> 02:57.480
But take a look at it.

02:57.490 --> 03:02.650
This is the one which represents the actual current machine's time.

03:02.650 --> 03:03.430
So.

03:04.250 --> 03:07.610
As I mentioned in the presentation, Jan first.

03:10.300 --> 03:11.620
1970.

03:13.140 --> 03:21.660
The skull epoch and each and every day has 86,400 seconds.

03:22.690 --> 03:23.290
Okay.

03:24.710 --> 03:28.820
Each and every day has 86,400 seconds.

03:28.850 --> 03:37.580
The first second of Jan first, 1970, which represents the value as zero from that point to this point.

03:37.610 --> 03:40.270
How many seconds that is this value.

03:40.340 --> 03:49.130
So the current machine's time is 2018, zero 719, and the time is ten, 14 and four zero.

03:49.250 --> 03:49.970
Right.

03:49.970 --> 03:55.160
And then let's go ahead and check the instant dot off method.

03:55.160 --> 03:57.380
There is a handy off method.

03:59.760 --> 04:02.940
And I'm going to pass zero as a value.

04:03.460 --> 04:05.230
They pass the zero as a value.

04:05.250 --> 04:07.910
This is going to give me the time as this one.

04:07.920 --> 04:13.530
Let's go ahead and check that because that is the first second which represents.

04:14.100 --> 04:14.790
The epic.

04:17.540 --> 04:18.740
Run this example.

04:22.060 --> 04:22.690
Take a look at it.

04:22.690 --> 04:24.190
It represents 19

04:24.190 --> 04:30.520
0101000000Z.

04:30.910 --> 04:31.330
So.

04:32.750 --> 04:35.720
This is the starting of the epoch.

04:36.550 --> 04:39.550
Now, let's say you have another instance.

04:40.830 --> 04:43.560
Of instant, instant, instant.

04:43.560 --> 04:44.910
1 or 2.

04:45.270 --> 04:46.440
Instant dot.

04:51.280 --> 04:57.160
If you want to find the difference between these two instant, this instant and this instant, there

04:57.160 --> 05:02.920
is a handy method called duration, which we used for the local time and local date time.

05:02.920 --> 05:03.190
Right?

05:03.190 --> 05:04.680
You can use the same thing here.

05:04.690 --> 05:07.690
Duration dot between.

05:08.470 --> 05:09.370
Instant.

05:11.030 --> 05:12.560
Comma, instant one.

05:14.090 --> 05:20.660
This is going to give you the difference between them and it is going to give you the duration as a

05:20.660 --> 05:21.620
response.

05:23.670 --> 05:25.440
Now what we are going to do.

05:26.540 --> 05:33.230
We're going to get the duration and represent that in nano form because there is not much difference

05:33.230 --> 05:35.750
between these two time instances.

05:35.750 --> 05:35.990
Right?

05:35.990 --> 05:40.400
Because this is created at line number ten and this is created at line number 18.

05:40.430 --> 05:43.340
The time difference between them is going to be very, very minimal.

05:43.340 --> 05:47.210
So in order to show some value, I'm going to represent that in nanoseconds.

05:48.420 --> 05:50.400
Gonna have difference here.

05:52.510 --> 05:54.670
Let's run this example and check the result.

05:57.120 --> 05:57.700
There you go.

05:57.720 --> 06:03.900
It gave you the time difference between these two instances as a nanosecond.

06:04.200 --> 06:07.200
So this is all about instant, which you have to know.

06:07.230 --> 06:13.080
The fundamental thing is instant is used to represent the time in a machine readable format.

06:13.800 --> 06:15.570
With this, we came to the end of this tutorial.
