WEBVTT

00:01.260 --> 00:01.800
Hi everyone.

00:01.800 --> 00:03.120
Welcome back to this tutorial.

00:03.120 --> 00:06.630
In this tutorial we will code and explore the period class.

00:06.630 --> 00:10.380
Before we get into the code, let me explain some theory about the period.

00:10.380 --> 00:18.030
Class period is a date based representation of time in days, months, years and is part of the Java

00:18.030 --> 00:20.610
dot time package period.

00:20.610 --> 00:23.670
Class is compatible with local date.

00:23.670 --> 00:29.160
Basically we can use period class along with the local date instance.

00:30.040 --> 00:30.370
Period.

00:30.370 --> 00:38.020
Class is used to represent a period of time, not just a specific date, which local date use to represent.

00:38.050 --> 00:39.550
What do I mean by that?

00:39.580 --> 00:43.750
Let's say you want to represent a period of ten days.

00:43.750 --> 00:48.550
Then you can use the period class to create a period of ten days.

00:48.580 --> 00:55.930
As you see in the example, using the off days factory method, let's say you want to represent a period

00:55.930 --> 00:56.870
of ten years.

00:56.890 --> 01:02.920
Then we can use the off years factory method that is part of the period class.

01:02.980 --> 01:08.680
Let's talk about the more generic use case where you can use the period class.

01:08.860 --> 01:16.420
The most generic use case is to calculate the difference between the two dates using the between method

01:16.420 --> 01:18.130
that is part of the period class.

01:18.160 --> 01:19.880
Let's go ahead and code this one.

01:19.900 --> 01:22.170
So here is an example.

01:22.180 --> 01:30.070
You have two dates here which which is 2018, zero one and zero one, which is the local date, and

01:30.070 --> 01:35.390
then the local date one which represents the date as 2018 zero 131.

01:35.410 --> 01:40.600
If we do a between these two dates, it will give you the days as a response.

01:40.600 --> 01:43.550
And this end date is end exclusive.

01:43.570 --> 01:47.620
Basically this is going to return you the result as 30 days.

01:47.650 --> 01:49.330
Let's go ahead and explore this one.

01:51.440 --> 01:52.220
What I'm going to do.

01:52.220 --> 01:53.510
I'm going to create.

01:55.570 --> 02:01.870
A class name called comparing dates, period.

02:02.050 --> 02:02.950
Example.

02:04.890 --> 02:09.540
Then I'm going to make this class executable by adding public static void main method.

02:11.020 --> 02:12.820
Let me put this in presentation mode.

02:14.070 --> 02:17.280
Now what I'm going to do, I'm going to create two dates.

02:18.410 --> 02:20.070
One once a local date.

02:21.130 --> 02:26.730
Give it a name and a local date dot off.

02:27.190 --> 02:29.560
I'm going to give the year 2018.

02:29.590 --> 02:33.010
I'm going to give January 1st as the local date.

02:33.040 --> 02:35.920
One instance meaning the local date instance.

02:36.100 --> 02:37.780
I'm going to create another date.

02:37.780 --> 02:42.610
I'm going to give it a name as a local date one, and this is going to be 12th month.

02:42.730 --> 02:43.600
And.

02:44.590 --> 02:47.650
31, which is the last day of the year.

02:48.010 --> 02:55.810
Now what we are going to do there is a handy method called until which is part of the local date instance

02:55.810 --> 02:56.560
itself.

02:57.190 --> 03:00.190
Local date dot until.

03:00.400 --> 03:04.800
If you do this until it passes another chrono local date.

03:04.810 --> 03:08.770
What I'm going to do I'm going to pass the.

03:10.120 --> 03:12.700
Localdate one as an input to that method.

03:13.950 --> 03:14.500
Right.

03:14.520 --> 03:16.880
What is this output from this method?

03:16.890 --> 03:18.060
Let's go ahead and check it.

03:18.180 --> 03:18.810
Take a look at it.

03:18.810 --> 03:20.970
The output is going to be a period.

03:21.450 --> 03:23.040
So let's go ahead and.

03:24.210 --> 03:26.520
Get that value and then give it a name.

03:26.910 --> 03:27.780
There you go.

03:28.050 --> 03:30.900
We we got the period as a output.

03:30.930 --> 03:35.160
Now let's explore what are the different methods that are available as part of the period.

03:37.210 --> 03:42.250
So what I'm going to do, I'm going to create I'm going to use a period and then check what are the

03:42.250 --> 03:44.330
different methods it supports.

03:44.350 --> 03:48.280
The first method which we are going to explore is a get days method.

03:48.640 --> 03:52.330
Let's see, what is the value that it is going to return.

03:55.270 --> 03:56.140
Run this one.

03:58.030 --> 04:00.820
The value it returned us 30.

04:01.330 --> 04:03.160
We take a look at it here.

04:03.160 --> 04:08.850
We have this date as 31st and we have the date as zero one.

04:08.860 --> 04:13.040
But if you take a look at the month, this is last day of the month and this is first day of the month.

04:13.060 --> 04:17.950
The one thing you have to keep in your mind is just compare these two days.

04:17.980 --> 04:19.560
It doesn't care about the month.

04:19.570 --> 04:27.430
So basically what it does is the result is 30 days, but it performs.

04:28.740 --> 04:30.420
31 minus one.

04:30.600 --> 04:33.540
Behind the scenes and then returning the result as 30.

04:33.870 --> 04:38.970
Let's go ahead and check what it returns for get months.

04:42.600 --> 04:44.410
I put get months here.

04:45.390 --> 04:47.790
Let's go ahead and run this and check the result.

04:48.710 --> 04:50.090
But take a look at the get months.

04:50.090 --> 04:51.380
The result is 11.

04:51.890 --> 04:54.710
So it performed 12 -11.

04:55.420 --> 04:59.380
Now let's go and check the period dot get years.

05:01.260 --> 05:02.310
Get years.

05:03.080 --> 05:09.110
This is going to give you the result as zero because both are of the same year.

05:10.410 --> 05:12.770
Brigham gave the result as zero.

05:12.810 --> 05:19.080
Now, these approaches are something which we have two instances of local date using which we can create

05:19.080 --> 05:19.980
a period instance.

05:19.980 --> 05:23.520
Let's say you want to create a period instance using a factory method.

05:23.520 --> 05:28.200
There are some handy factory methods which you can use in order to create the same period.

05:28.200 --> 05:28.950
Instance.

05:28.950 --> 05:31.650
Let's say you want to create a period.

05:33.550 --> 05:35.940
Dart off as soon as you type in off.

05:35.950 --> 05:37.580
There are multiple options here.

05:37.600 --> 05:41.500
What I'm going to do, I'm going to want to create a period of ten days.

05:41.980 --> 05:44.970
So this will give you the period as an instance.

05:44.980 --> 05:46.030
Give it a name.

05:47.630 --> 05:50.750
And then let's check the days in it.

05:53.210 --> 05:54.110
Go and check it.

05:56.300 --> 05:57.950
Not get this.

05:59.820 --> 06:06.900
We all know it has a method called gay days, which is going to return the days as a output.

06:07.850 --> 06:08.250
There you go.

06:08.730 --> 06:14.610
Let's say you want to create a period of 20 years or ten years.

06:16.360 --> 06:16.780
Right.

06:16.780 --> 06:20.290
So then we can use the off years method.

06:20.500 --> 06:23.620
I'm going to give it a name as 20.

06:24.010 --> 06:25.570
Now let's check.

06:27.200 --> 06:32.660
We all know it is going to give you the when you perform period or get years, it's going to give you

06:32.660 --> 06:33.410
the result as ten.

06:33.410 --> 06:39.200
But still, I would like to check it period two dot get years.

06:39.200 --> 06:42.440
This is going to return ten years as an output.

06:44.430 --> 06:48.180
Okay, Let's say you want to calculate how many months.

06:48.180 --> 06:50.310
Let's run this and then check the result.

06:52.530 --> 06:53.270
Here you go.

06:53.340 --> 06:55.720
It returned the years as ten years.

06:55.740 --> 07:00.370
Now you want to calculate how many months are there in those ten years?

07:00.390 --> 07:05.340
We all know the answer is going to be 120, but how do you systematically calculate it?

07:05.490 --> 07:08.070
There is a method called two total months.

07:08.100 --> 07:12.690
This will give you the total number of months.

07:13.820 --> 07:15.140
In ten years.

07:16.980 --> 07:17.580
There you go.

07:17.610 --> 07:22.350
If you take a look at it, there is no such method, like days, two days or something like that.

07:22.440 --> 07:26.370
So if you want to do two days, then you have to manually calculate it.

07:26.400 --> 07:30.060
It doesn't have any factory method or method that is part of the.

07:31.260 --> 07:32.520
Period object.

07:32.610 --> 07:36.540
Now what we are going to do, we are going to explore the between method.

07:36.570 --> 07:43.740
So here we have used the until method which gave you the result as the period using which we were able

07:43.740 --> 07:46.140
to get the days, months and years.

07:46.170 --> 07:48.210
There is there is another handy method.

07:48.210 --> 07:50.520
There is a factory method which is part of the.

07:52.070 --> 07:53.480
It'll class itself.

07:53.780 --> 07:57.410
We take a look at it until it's part of the local date instance.

07:57.410 --> 07:57.880
Right?

07:57.890 --> 08:01.010
Meaning local date class has this until method.

08:01.010 --> 08:06.140
So there is a method which is part of the period itself which you can use in order to compare two different

08:06.140 --> 08:06.710
dates.

08:07.100 --> 08:12.950
So between what I'm going to do, I'm going to give the local date and local date one as a input to

08:12.950 --> 08:13.160
it.

08:13.600 --> 08:17.300
So I'm going to give local date and local date one.

08:19.280 --> 08:19.610
Okay.

08:19.610 --> 08:25.490
And then this is going to return period and period.

08:26.940 --> 08:27.750
Give it a name.

08:27.900 --> 08:30.180
Now I'm going to print.

08:31.410 --> 08:32.310
The period.

08:36.360 --> 08:39.270
And let's print the days, months and years.

08:39.690 --> 08:44.070
Period three dot get days.

08:44.400 --> 08:48.450
Want to separate this one by colon Plus.

08:49.320 --> 08:49.830
Period.

08:51.000 --> 08:55.740
Three dot get months and then.

08:56.700 --> 08:58.050
Separated by a colon.

08:59.970 --> 09:02.970
Period three dot get.

09:03.870 --> 09:04.530
Here's.

09:04.980 --> 09:07.320
Lets go ahead and run this and check the result.

09:10.320 --> 09:10.900
Here you go.

09:10.920 --> 09:13.350
It printed the result as expected.

09:13.350 --> 09:14.580
3011 zero.

09:14.610 --> 09:16.140
That's what we have here too.

09:16.170 --> 09:18.460
3011 and zero.

09:18.480 --> 09:19.470
Now.

09:20.440 --> 09:23.380
Period is only compatible with local data.

09:23.410 --> 09:26.170
That's what I mentioned in the presentation.

09:26.170 --> 09:26.500
Right.

09:26.500 --> 09:27.610
Let's go ahead and explore.

09:27.640 --> 09:35.380
If we want to check like difference between two different times local time, let's create a local time

09:35.380 --> 09:35.980
instance.

09:35.980 --> 09:38.560
Dot Now is what I'm going to do.

09:38.560 --> 09:39.790
I'm going to call.

09:39.820 --> 09:42.940
Period dot between.

09:43.960 --> 09:46.750
Local time dot now.

09:47.980 --> 09:50.470
And then let's import this.

09:52.300 --> 09:54.130
And then local time.

09:55.640 --> 09:59.360
Dot now dot plus.

10:01.310 --> 10:02.030
These.

10:02.060 --> 10:04.850
Let's use a prejudice or.

10:06.030 --> 10:07.030
Blazers, right?

10:09.940 --> 10:10.650
Those are.

10:10.660 --> 10:13.200
So I'm going to add it 24 hours.

10:13.770 --> 10:14.370
Take a look at it.

10:14.370 --> 10:15.840
It is not compatible.

10:15.840 --> 10:21.240
Let's go ahead and check what is this between method accepts the between method accepts only the local

10:21.240 --> 10:25.680
date as a input so it doesn't accept the local time.

10:25.680 --> 10:33.630
And if you take a look at the methods, let's take a look at the period three dot get doesn't have any

10:33.630 --> 10:39.780
of the time related methods that will give you the time information of a date actually.

10:39.780 --> 10:45.120
So this period is only compatible with local date instance.

10:45.150 --> 10:50.760
If you want to perform comparison of times, there is a handy class called duration, which I'll be

10:50.760 --> 10:52.320
covering in the next tutorial.

10:53.490 --> 10:55.350
So this is all about period.

10:55.920 --> 10:58.650
And with this, we came to the end of this tutorial.

10:58.770 --> 10:59.850
Thank you for watching.
