Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the period class.
Before we get into the code, let me explain some theory about the period.
Class period is a date based representation of time in days, months, years and is part of the Java
dot time package period.
Class is compatible with local date.
Basically we can use period class along with the local date instance.
Period.
Class is used to represent a period of time, not just a specific date, which local date use to represent.
What do I mean by that?
Let's say you want to represent a period of ten days.
Then you can use the period class to create a period of ten days.
As you see in the example, using the off days factory method, let's say you want to represent a period
of ten years.
Then we can use the off years factory method that is part of the period class.
Let's talk about the more generic use case where you can use the period class.
The most generic use case is to calculate the difference between the two dates using the between method
that is part of the period class.
Let's go ahead and code this one.
So here is an example.
You have two dates here which which is 2018, zero one and zero one, which is the local date, and
then the local date one which represents the date as 2018 zero 131.
If we do a between these two dates, it will give you the days as a response.
And this end date is end exclusive.
Basically this is going to return you the result as 30 days.
Let's go ahead and explore this one.
What I'm going to do.
I'm going to create.
A class name called comparing dates, period.
Example.
Then I'm going to make this class executable by adding public static void main method.
Let me put this in presentation mode.
Now what I'm going to do, I'm going to create two dates.
One once a local date.
Give it a name and a local date dot off.
I'm going to give the year 2018.
I'm going to give January 1st as the local date.
One instance meaning the local date instance.
I'm going to create another date.
I'm going to give it a name as a local date one, and this is going to be 12th month.
And.
31, which is the last day of the year.
Now what we are going to do there is a handy method called until which is part of the local date instance
itself.
Local date dot until.
If you do this until it passes another chrono local date.
What I'm going to do I'm going to pass the.
Localdate one as an input to that method.
Right.
What is this output from this method?
Let's go ahead and check it.
Take a look at it.
The output is going to be a period.
So let's go ahead and.
Get that value and then give it a name.
There you go.
We we got the period as a output.
Now let's explore what are the different methods that are available as part of the period.
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
different methods it supports.
The first method which we are going to explore is a get days method.
Let's see, what is the value that it is going to return.
Run this one.
The value it returned us 30.
We take a look at it here.
We have this date as 31st and we have the date as zero one.
But if you take a look at the month, this is last day of the month and this is first day of the month.
The one thing you have to keep in your mind is just compare these two days.
It doesn't care about the month.
So basically what it does is the result is 30 days, but it performs.
31 minus one.
Behind the scenes and then returning the result as 30.
Let's go ahead and check what it returns for get months.
I put get months here.
Let's go ahead and run this and check the result.
But take a look at the get months.
The result is 11.
So it performed 12 -11.
Now let's go and check the period dot get years.
Get years.
This is going to give you the result as zero because both are of the same year.
Brigham gave the result as zero.
Now, these approaches are something which we have two instances of local date using which we can create
a period instance.
Let's say you want to create a period instance using a factory method.
There are some handy factory methods which you can use in order to create the same period.
Instance.
Let's say you want to create a period.
Dart off as soon as you type in off.
There are multiple options here.
What I'm going to do, I'm going to want to create a period of ten days.
So this will give you the period as an instance.
Give it a name.
And then let's check the days in it.
Go and check it.
Not get this.
We all know it has a method called gay days, which is going to return the days as a output.
There you go.
Let's say you want to create a period of 20 years or ten years.
Right.
So then we can use the off years method.
I'm going to give it a name as 20.
Now let's check.
We all know it is going to give you the when you perform period or get years, it's going to give you
the result as ten.
But still, I would like to check it period two dot get years.
This is going to return ten years as an output.
Okay, Let's say you want to calculate how many months.
Let's run this and then check the result.
Here you go.
It returned the years as ten years.
Now you want to calculate how many months are there in those ten years?
We all know the answer is going to be 120, but how do you systematically calculate it?
There is a method called two total months.
This will give you the total number of months.
In ten years.
There you go.
If you take a look at it, there is no such method, like days, two days or something like that.
So if you want to do two days, then you have to manually calculate it.
It doesn't have any factory method or method that is part of the.
Period object.
Now what we are going to do, we are going to explore the between method.
So here we have used the until method which gave you the result as the period using which we were able
to get the days, months and years.
There is there is another handy method.
There is a factory method which is part of the.
It'll class itself.
We take a look at it until it's part of the local date instance.
Right?
Meaning local date class has this until method.
So there is a method which is part of the period itself which you can use in order to compare two different
dates.
So between what I'm going to do, I'm going to give the local date and local date one as a input to
it.
So I'm going to give local date and local date one.
Okay.
And then this is going to return period and period.
Give it a name.
Now I'm going to print.
The period.
And let's print the days, months and years.
Period three dot get days.
Want to separate this one by colon Plus.
Period.
Three dot get months and then.
Separated by a colon.
Period three dot get.
Here's.
Lets go ahead and run this and check the result.
Here you go.
It printed the result as expected.
3011 zero.
That's what we have here too.
3011 and zero.
Now.
Period is only compatible with local data.
That's what I mentioned in the presentation.
Right.
Let's go ahead and explore.
If we want to check like difference between two different times local time, let's create a local time
instance.
Dot Now is what I'm going to do.
I'm going to call.
Period dot between.
Local time dot now.
And then let's import this.
And then local time.
Dot now dot plus.
These.
Let's use a prejudice or.
Blazers, right?
Those are.
So I'm going to add it 24 hours.
Take a look at it.
It is not compatible.
Let's go ahead and check what is this between method accepts the between method accepts only the local
date as a input so it doesn't accept the local time.
And if you take a look at the methods, let's take a look at the period three dot get doesn't have any
of the time related methods that will give you the time information of a date actually.
So this period is only compatible with local date instance.
If you want to perform comparison of times, there is a handy class called duration, which I'll be
covering in the next tutorial.
So this is all about period.
And with this, we came to the end of this tutorial.
Thank you for watching.